﻿using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.IO;
using System.IO.Ports;
using System.Reflection;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug
{
    public partial class rm69310 : Form
    {
        //---------------------R66455 the dll function define-----------------------------------
        //De_Mura
        byte[] Erase_Flash_SyR66455_601_SSD =
        {
            0x0d,0x01,0x40,0xc0,0x01,0x00,0xb7,0x07,0x42,0xb1,
            0x01,0x01,0xb2,0x05,0x1b,0xb3,0x44,0x59,0xb4,0x04,
            0x38,0xb5,0x08,0x70,0xb6,0x00,0x37,0xde,0x00,0x03,
            0xb9,0x00,0x00,0xba,0xc2,0x50,0xbb,0x00,0x0c,0xd6,
            0x00,0x05,0xb9,0x00,0x01,0xb8,0x00,0x00,0xc9,0x1b,
            0x09,0xca,0x3a,0x0c,0xcb,0x03,0x1b,0xcc,0x0f,0x14,
            0xdf,0x00,0x3d,0xb7,0x07,0x4b,0xff,0x98,0x21
        };

        byte[] SyR66455_621_DeMura_ON_Part_0 =
        {
            0x30,0x01,0x02,0xb0,0x80,0x8a,0xb3
        };

        byte[] SyR66455_621_DeMura_ON_Part_1 =
        {
            0x30,0x01,0x3c,0xd4,0x93,0x90,0x40,0x1e,0xe1,0x04,
            0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,
            0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,
            0x00,0x04,0x00,0x0f,0xff,0x00,0x00,0x0f,0xff,0x00,
            0x00,0x0f,0xff,0x00,0x00,0x03,0x20,0x00,0xdf,0x00,
            0xc8,0x01,0x00,0x4d,0x8b,0x00,0x4d,0x8b,0x00,0x4d,
            0x8b,0x03,0x38,0xea,0x03
        };

        byte[] SyR66455_621_DeMura_OFF_Part_0 =
{
            0x30,0x01,0x02,0xb0,0x80,0x8a,0xb3
        };

        byte[] SyR66455_621_DeMura_OFF_Part_1 =
        {
            0x30,0x01,0x3c,0xd4,0x90,0x90,0x40,0x1e,0xe1,0x04,
            0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,
            0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,0x00,0x04,
            0x00,0x04,0x00,0x0f,0xff,0x00,0x00,0x0f,0xff,0x00,
            0x00,0x0f,0xff,0x00,0x00,0x03,0x20,0x00,0xdf,0x00,
            0xc8,0x01,0x00,0x4d,0x8b,0x00,0x4d,0x8b,0x00,0x4d,
            0x8b,0x03,0x38,0x15,0xc0
        };

        byte[] SyR66455_Read_DeMura_Flash_ID_0 =
        {
            0x0d,0x02,0x48,0x02,0xb0,0x00,0x33,0xdf,0x00,0x00,
            0x58,0x83,0x2d,0x05,0x35,0x15,0x01,0x31,0x11,0x0b,
            0x50,0x00,0x02,0x00,0x00,0x43,0x84,0x00,0x10,0x00,
            0xdf,0xd4,0x0e,0x00,0x00,0x13,0xff,0x8c,0x27,0x17,
            0x0f,0x00,0x55,0x18,0x0f,0x00,0x1a,0x0f,0x00,0x1c,
            0x0f,0x00,0x1e,0x0f,0x00,0x00,0x00,0x00,0x06,0xf3,
            0x9f,0x00,0x00,0x00,0x02,0x02,0xf2,0x09,0x02,0x37,
            0x03,0x02,0x14,0xf4,0xff,0xd6,0xbe
        };

        byte[] SyR66455_Read_DeMura_Flash_ID_1 =
        {
            0x30,0x02,0x04,0xf4,0x00,0x00,0x00,0xff,0xb9
        };

        byte[] SyR66455_Read_DeMura_Flash_ID_2 =
        {
            0x0d,0x03,0x03,0xb7,0x07,0x4b,0x2e,0x0a
        };
        byte[] SyR66455_Read_DeMura_Flash_ID_3 =
        {
            0x0a,0x07,0x15,0xff,0xff,0xff,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0xd5,0x70
        };
        byte[] SyR66455_Read_DeMura_Flash_ID_4 =
        {
            0x0d,0x04,0x02,0x14,0xf4,0xf4,0xcb
        };

        byte[] Test1 =
        {
            0x30,0x01,0x02,0xb0,0x00,0x1b,0x3b
        };
        byte[] Test2 =
        {
            0x30,0x01,0x33,0xdf,0x00,0x00,0x58,0x83,0x2d,0x05,
            0x35,0x15,0x01,0x31,0x11,0x0b,0x50,0x00,0x02,0x00,
            0x00,0x43,0x84,0x00,0x10,0x00,0xdf,0xd4,0x0e,0x00,
            0x00,0x13,0xff,0x8c,0x27,0x17,0x0f,0x00,0x55,0x18,
            0x0f,0x00,0x1a,0x0f,0x00,0x1c,0x0f,0x00,0x1e,0x0f,
            0x00,0x00,0x00,0x00,0x82,0xec
        };
        byte[] Test3 =
        {
            0x30,0x01,0x06,0xf3,0x9f,0x00,0x00,0x00,0x02,0x36,
            0xeb
        };
        byte[] Test4 =
        {
            0x30,0x01,0x02,0xf2,0x09,0xe1,0xbc
        };
        byte[] Test5 =
        {
            0x30,0x01,0x02,0x37,0x03,0xa9,0x57
        };
        byte[] Test6 =
        {
            0x30,0x01,0x02,0x14,0xf4,0x65,0x1a
        };
        byte[] Test7 =
        {
            0x30,0x02,0x04,0xf4,0x00,0x00,0x00,0xff,0xb9
        };

        byte[] SyR66455_Read_DeMura_CheckSum_0 =
{
            0x30,0x01,0x02,0xb0,0x00,0x1b,0x3b
        };
        byte[] SyR66455_Read_DeMura_CheckSum_1 =
        {
            0x30,0x01,0x01,0x0b,0x1d,0x43
        };
        byte[] SyR66455_Read_DeMura_CheckSum_2 =
        {
            0x30,0x02,0x04,0xe7,0x00,0x00,0x00,0x7f,0xc2
        };




        byte[] Erase_Flash_SyR66455_601_MIPI =
        {
            0x0d,0x12,0x1c,0x02,0xb0,0x00,0x33,0xdf,0x00,0x40,
            0x58,0x83,0x2d,0x05,0x35,0x15,0x01,0x31,0x11,0x6b,
            0x50,0x00,0x02,0x00,0x00,0x43,0x84,0x00,0x10,0x00,
            0xdf,0xd4,0x0e,0x00,0x00,0x13,0xff,0x8c,0x27,0x17,
            0x0f,0x00,0x55,0x18,0x0f,0x00,0x1a,0x0f,0x00,0x1c,
            0x0f,0x00,0x1e,0x0f,0x00,0x00,0x00,0x00,0x02,0xb0,
            0x00,0x06,0xf3,0x06,0x00,0x00,0x00,0x00,0x04,0xff,
            0xff,0xff,0x0a,0x02,0xf2,0x11,0x02,0xb0,0x00,0x06,
            0xf3,0x05,0x00,0x00,0x00,0x00,0x04,0xff,0xff,0xff,
            0x0a,0x02,0xf2,0x09,0x02,0xb0,0x00,0x02,0x37,0x02,
            0x04,0xff,0xff,0xff,0x0a,0x02,0x14,0xf4,0x04,0xff,
            0xff,0xff,0x0a,0x02,0xb0,0x00,0x06,0xf3,0x60,0x00,
            0x00,0x00,0x00,0x04,0xff,0xff,0xff,0x0a,0x02,0xf2,
            0x11,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,0xff,0xff,
            0xfa,0x04,0xff,0xff,0xff,0xfa,0xff,0x2b,0xf0,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
            0x00,0x00,0x00,0x00,0x00,0x00,0x00
        };
        byte[] Erase_Flash_SyR66455_601_MIPI_1 =
        {
            0x0d,0x02,0x58,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,
            0xff,0xff,0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,
            0xff,0xff,0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,
            0xff,0xff,0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,
            0xff,0xff,0xfa,0x04,0xff,0xff,0xff,0xfa,0x04,0xff,
            0xff,0xff,0xfa,0x02,0xb0,0x00,0x06,0xf3,0x05,0x00,
            0x00,0x00,0x00,0x04,0xff,0xff,0xff,0x0a,0x02,0xf2,
            0x09,0x02,0xb0,0x04,0x02,0x37,0x02,0x04,0xff,0xff,
            0xff,0x0a,0x02,0x14,0xf4,0x04,0xff,0xff,0xff,0x0a,
            0xff,0x07,0x3e
        };
        byte[] SyR66455_601_Send_DeMura_Data_MIPI_Init =//初始化部分
        {
            0x30,0x0f,0x02,0xb0,0x00,0xB9,0x61
        };
        byte[] SyR66455_601_Send_DeMura_Data_MIPI_0 =//初始化部分
        {
            0x30,0x01,0x02,0xb0,0x00,0x1b,0x3b
        };
        byte[] SyR66455_601_Send_DeMura_Data_MIPI_1 =//初始化部分
        {
            0x30,0x01,0x33,0xdf,0x00,0x40,0x58,0x83,0x2d,0x05,
            0x35,0x15,0x01,0x31,0x11,0x6b,0x50,0x00,0x02,0x00,
            0x00,0x43,0x84,0x00,0x10,0x00,0xdf,0xd4,0x0e,0x00,
            0x00,0x13,0xff,0x8c,0x27,0x17,0x0f,0x00,0x55,0x18,
            0x0f,0x00,0x1a,0x0f,0x00,0x1c,0x0f,0x00,0x1e,0x0f,
            0x00,0x00,0x00,0x00,0xa0,0x0c
        };
        byte[] SyR66455_601_Send_DeMura_Data_MIPI_2 =//循环部分
        {
            0x30,0x01,0x02,0xb0,0x04,0x5b,0xbf
        };
        byte[] SyR66455_601_Send_DeMura_Data_MIPI_3 =//循环部分
        {
            0x30,0x01,0x06,0xf3,0x06,0x00,0x00,0x00,0x00,0x98,
            0x5f
        };
        byte[] SyR66455_601_Send_DeMura_Data_MIPI_4 =//循环部分
        {
            0x30,0x01,0x02,0xf2,0x11,0x72,0x85
        };
        byte[] SyR66455_601_Send_DeMura_Data_MIPI_5 =//循环部分_变化_数据地址
        {
            0x30,0x01,0x06,0xf3,0x02,0x00,0x0f,0x00,0x7f,0xb2,
            0x10
        };
        byte[] SyR66455_601_Send_DeMura_Data_MIPI_6 =//循环部分_数据结尾
{
            0x30,0x01,0x02,0xf2,0x1b,0xd3,0xcf
        };

        /// <summary>
        /// ///rm69310  
        /// </summary>
        private double B_x = 0.14;
        private double B_y = 0.06;
        private int[] RM69310_GAMMA_GRAY = new int[25];
        int[] GAMMA_69310_Point25_Buf = new int[25] { 0, 7, 11, 15, 19, 23, 27, 31, 35, 39, 47, 55, 63, 79, 95, 111, 127, 143, 159, 175, 191, 207, 223, 239, 255 };
        int[] GAMMA_69310_Point10_Buf = new int[11] { 0, 7, 11, 15, 31, 63, 95, 127, 159, 191, 255 };
        int[] GAMMA_69310_Point25_BufdataR = new int[25];
        int[] GAMMA_69310_Point25_BufdataG = new int[25];
        int[] GAMMA_69310_Point25_BufdataB = new int[25];




        //De_Mura
        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke  
        //==============ca310===============
        CaSDK Ca310Obj = new CaSDK();
        private double auto_lum;//
        private double auto_x;
        private double auto_y;
        private double auto_T;
        private double auto_duv;
        private bool ca310calFlag = false;
        private int[] tmp_gamma_195 = new int[93];
        private int[] tmp_gamma_160 = new int[33];
        private int[] tmp_gamma_162 = new int[75];
        private int[] tmp_gamma_120 = new int[78];
        private int[] tmp_gamma_010 = new int[75];
        private int[] tmp_gamma_350 = new int[75];
        private int[] tmp_gamma_300 = new int[75];


        /// <summary>
        /// /auto gamma add 20181031
        /// </summary>
        private int flag1 = 1;
        private int flag2 = 1;
        private int flag3 = 1;
        private Boolean flag_R = true;
        private Boolean flag_G = true;
        private Boolean flag_B = true;

        //private ushort[] gamma_r = new ushort[31];
        //private ushort[] gamma_g = new ushort[31];
        //private ushort[] gamma_b = new ushort[31];

        private ushort[] mygamma_r = new ushort[28];
        private ushort[] mygamma_g = new ushort[28];
        private ushort[] mygamma_b = new ushort[28];


        private int step_h = 12;
        private int step_l = 1;
        //==============ca310===============

        private int CAS200_Measure_Gray = 255;
        private int CAS200_Measure_Count = 0;
        private int CA200_Measure_CE = 0;
        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过   
        private byte[] Rec_tmp = new byte[1];//
        private byte[] Rec_tmp1 = new byte[1];//
        private byte[] Send_buf = new byte[1];
        private byte signal_mode;
        private byte start_trans_flag = 0;
        private byte continueflag = 0;

        public byte RM350_0XFE_XX = 0;

        private byte[] Driver_IC = new byte[1];
        private List<string> ImageType;
        private delegate void MonitorSerialPortChangeEventHandler();
        private MonitorSerialPortChangeEventHandler MonitorCom;
        private int serialPortStatus;

        CRC16 CRCData = new CRC16();

        //public frmStm32updata1(frmMain Frm)
        //{
        //    MainForm = Frm;
        //    InitializeComponent();
        //}

        private static bool Delay(int delayTime)
        {
            DateTime now = DateTime.Now;
            int s;
            do
            {
                TimeSpan spand = DateTime.Now - now;
                s = spand.Seconds;
                Application.DoEvents();
            }
            while (s < delayTime);
            return true;
        }


        frmMain MainForm = null;
        public rm69310(frmMain Frm)
        {
            MainForm = Frm;
            InitializeComponent();
        }
        //private void System.Windows.Forms.(object sender, EventArgs e)
        //{
        //    MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
        //}

        private void Frm_Activated(object sender, EventArgs e)
        {

        }

        //是否已经绑定了事件        
        private bool IsBindEvent(Type type, Control con, string eventName)
        {
            bool isBind = false;

            PropertyInfo pi = type.GetProperty("Events", BindingFlags.Instance | BindingFlags.NonPublic);   //获取type类定义的所有事件的信息
            EventHandlerList ehl = (EventHandlerList)pi.GetValue(con, null);    //获取con对象的事件处理程序列表
            FieldInfo fieldInfo = (typeof(Control)).GetField("EventText", BindingFlags.Static | BindingFlags.NonPublic); //获取Control类Click事件的字段信息
            Delegate d = ehl[fieldInfo.GetValue(null)];
            if (d == null)
            {
                return isBind;
            }
            foreach (Delegate del in d.GetInvocationList())
            {
                if (del.Method.Name == eventName)
                {
                    isBind = true;
                    break;
                }
            }
            return isBind;
        }
        private void asdftest_dfa(object sender, EventArgs e)
        {
            // bool isBind = IsBindEvent(MainForm.comPort.GetType(), MainForm.comPort, "comboBox_TextChanged");
            //为Button1绑定一个事件处理程序
            Button btn = new Button();
            btn.Click += new
            EventHandler(Frm_Activated);
            //获取Button类定义的所有事件的信息
            PropertyInfo pi =
            (typeof(Button)).GetProperty("Events", BindingFlags.Instance |
            BindingFlags.NonPublic);



        }

        //{
        //    if (!isBind)

        //    {
        //         MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
        //    }
        //}
        //=================================Timer====Start=========================
        private void T1_Tick(object sender, EventArgs e)
        {
            T1.Enabled = false;
            textBox1.Text = textBox1.Text + "通讯故障!\r\n";
            textBox1.Focus();//获取焦点
            textBox1.Select(textBox1.TextLength, 0);//光标定位到文本最后
            textBox1.ScrollToCaret();//滚动到光标处
        }
        private void T2_Tick(object sender, EventArgs e)
        {

            toolStripStatusLabel2.Text = DateTime.Now.ToString();
        }
        //=================================Timer====end=========================
        private void SerialPort1_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            //if (MainForm.IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = MainForm.comPort.BytesToRead;
                byte[] buf = new byte[n];
                byte data;

                MainForm.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    if (buffer[0] == 0x0B)          //返回 STM32内部 flash 的配置文件数据
                    {
                        if (buffer.Count >= (Int16)(buffer[2] << 8) + (buffer[3]) + 7)
                        {
                            Rec_tmp = new byte[(Int16)(buffer[2] << 8) + (buffer[3]) + 7];
                            byte[] crcTemp = new byte[(Int16)(buffer[2] << 8) + (buffer[3]) + 7];
                            buffer.CopyTo(0, crcTemp, 0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[(Int16)(buffer[2] << 8) + (buffer[3]) + 7 - 2] != CRCH && buffer[(Int16)(buffer[2] << 8) + (buffer[3]) + 7 - 1] != CRCL) //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, (Int16)(buffer[2] << 8) + (buffer[3]) + 7);//正确分析一条数据，从缓存中移除数据。
                            }

                        }
                    }
                    else if (buffer[0] == 0xFE)
                    {
                        Rec_tmp = new byte[buffer[2] + 5];
                        byte[] crcTemp = new byte[buffer[2] + 5];
                        buffer.CopyTo(0, crcTemp, 0, buffer[2] + 5);

                        CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                        if (buffer[buffer[2] + 5 - 2] != CRCH && buffer[buffer[2] + 5 - 1] != CRCL)     //CRC校验出错
                        {
                            buffer.RemoveRange(0, buffer.Count);
                        }
                        else
                        {
                            buffer.CopyTo(0, Rec_tmp, 0, buffer[2] + 5);//复制一条完整数据到具体的数据缓存   
                            data_1_catched = true;
                            buffer.RemoveRange(0, buffer[2] + 5);//正确分析一条数据，从缓存中移除数据。
                            if (Rec_tmp[4] == 0)
                                continueflag = 1;
                        }
                    }
                    else if (buffer[0] == 0x55)     //STM32 更新bin文件成功  返回
                    {
                        if (buffer.Count >= (buffer[2] + 5))
                        {
                            Rec_tmp = new byte[(buffer[2] + 5)];
                            byte[] crcTemp = new byte[(buffer[2] + 5)];
                            buffer.CopyTo(0, crcTemp, 0, (buffer[2] + 5));

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[(buffer[2] + 5) - 2] != CRCH && buffer[(buffer[2] + 5) - 1] != CRCL)       //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, (buffer[2] + 5));//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, (buffer[2] + 5));//正确分析一条数据，从缓存中移除数据。
                                switch (Rec_tmp[3])
                                {
                                    case 0x00:
                                        {
                                            start_trans_flag = 1;
                                        }
                                        break;
                                    case 0x01:
                                        {
                                            if (MainForm.comPort.IsOpen == true) MainForm.comPort.Close();
                                        }
                                        break;
                                    case 0x02:
                                        {
                                            start_trans_flag = 0;
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }

                        }
                    }
                    else if (((buffer[0] == 0x1E) || (buffer[0] == 0x0E) || (buffer[0] == 0x0D) || (buffer[0] == 0x0A) || (buffer[0] == 0x09) || (buffer[0] == 0x08) || (buffer[0] == 0x19) || (buffer[0] == 0x29) || (buffer[0] == 0x2A) || (buffer[0] == 0x09) || (buffer[0] == 0x2E) || (buffer[0] == 0x2B) || (buffer[0] == 0x2C) || (buffer[0] == 0x2D) || (buffer[0] == 0x2F) || (buffer[0] == 0x30) || (buffer[0] == 0x31) || (buffer[0] == 0x3C) || (buffer[0] == 0x3D) || (buffer[0] == 0x4A)
                        || (buffer[0] == 0x4C) || (buffer[0] == 0x52)) && (Setting.TempRecord.cmdLen > 0))
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);//正确分析一条数据，从缓存中移除数据。 
                            }
                        }
                    }
                    else
                        buffer.RemoveRange(0, buffer.Count);
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        T1.Enabled = false; //停止超时计数

                        //------------------------------------------------------------------------------------------------------------------------
                        if ((Rec_tmp[0] == 0x09) || (Rec_tmp[0] == 0x19) || (Rec_tmp[0] == 0x29) || (Rec_tmp[0] == 0x2e))  //RM67195 & RM67295 & RM67198&RM69298
                        {
                            ;

                        }

                        ////---------------------------------------------------RM67160/RM67162/RM69310---------------------------------------
                        else if ((Rec_tmp[0] == 0x0E) || (Rec_tmp[0] == 0x1E) || (Rec_tmp[0] == 0x52))  //RM67160/RM67162/RM69310
                        {
                            string tmp = "";
                            switch (Rec_tmp[1])
                            {
                                case 0x01:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            textBox1.Text = textBox1.Text + "寄存器\t写 OK！\r\n";
                                    }
                                    break;

                                case 0x02:
                                    {
                                        if (Setting.TempRecord.RM67160CmdType == 0x01)  //register read
                                        {
                                            //string tmp = "";
                                            tmp = String.Format("{0:X2}", Rec_tmp[4]);//16进制
                                            txtRM67160Data.Text = tmp;
                                            textBox1.Text = textBox1.Text + "寄存器\t读 OK！\r\n";
                                        }
                                        if (Setting.TempRecord.RM69310CmdType == 0x01)  //register read
                                        {
                                            //string tmp = "";
                                            tmp = String.Format("{0:X2}", Rec_tmp[4]);//16进制
                                            txtRM67160Data.Text = tmp;
                                            textBox1.Text = textBox1.Text + "寄存器\t读 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x03://///write gamma ok  RM69310
                                    textBox1.Text = textBox1.Text + "寄gamma存器\t写 OK！\r\n";
                                    break;

                                case 0x04://///read gamma ok  RM69310
                                    textBox1.Text = textBox1.Text + "读gamma寄存器\t 读 OK！\r\n";
                                    rm69310_gammaR0.Value = Decimal.Parse((Rec_tmp[0x04 + 0x00] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x01].ToString());
                                    rm69310_gammaR1.Value = Decimal.Parse((Rec_tmp[0x04 + 0x02] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x03].ToString());
                                    rm69310_gammaR2.Value = Decimal.Parse((Rec_tmp[0x04 + 0x04] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x05].ToString());
                                    rm69310_gammaR3.Value = Decimal.Parse((Rec_tmp[0x04 + 0x06] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x07].ToString());
                                    rm69310_gammaR4.Value = Decimal.Parse((Rec_tmp[0x04 + 0x08] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x09].ToString());
                                    rm69310_gammaR5.Value = Decimal.Parse((Rec_tmp[0x04 + 0x0A] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x0B].ToString());
                                    rm69310_gammaR6.Value = Decimal.Parse((Rec_tmp[0x04 + 0x0C] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x0D].ToString());
                                    rm69310_gammaR7.Value = Decimal.Parse((Rec_tmp[0x04 + 0x0E] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x0F].ToString());
                                    rm69310_gammaR8.Value = Decimal.Parse((Rec_tmp[0x04 + 0x10] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x11].ToString());
                                    rm69310_gammaR9.Value = Decimal.Parse((Rec_tmp[0x04 + 0x12] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x13].ToString());
                                    rm69310_gammaR10.Value = Decimal.Parse((Rec_tmp[0x04 + 0x14] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x15].ToString());
                                    rm69310_gammaR11.Value = Decimal.Parse((Rec_tmp[0x04 + 0x16] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x17].ToString());
                                    rm69310_gammaR12.Value = Decimal.Parse((Rec_tmp[0x04 + 0x18] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x19].ToString());
                                    rm69310_gammaR13.Value = Decimal.Parse((Rec_tmp[0x04 + 0x1A] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x1B].ToString());
                                    rm69310_gammaR14.Value = Decimal.Parse((Rec_tmp[0x04 + 0x1C] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x1D].ToString());
                                    rm69310_gammaR15.Value = Decimal.Parse((Rec_tmp[0x04 + 0x1E] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x1F].ToString());
                                    rm69310_gammaR16.Value = Decimal.Parse((Rec_tmp[0x04 + 0x20] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x21].ToString());
                                    rm69310_gammaR17.Value = Decimal.Parse((Rec_tmp[0x04 + 0x22] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x23].ToString());
                                    rm69310_gammaR18.Value = Decimal.Parse((Rec_tmp[0x04 + 0x24] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x25].ToString());
                                    rm69310_gammaR19.Value = Decimal.Parse((Rec_tmp[0x04 + 0x26] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x27].ToString());
                                    rm69310_gammaR20.Value = Decimal.Parse((Rec_tmp[0x04 + 0x28] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x29].ToString());
                                    rm69310_gammaR21.Value = Decimal.Parse((Rec_tmp[0x04 + 0x2A] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x2B].ToString());
                                    rm69310_gammaR22.Value = Decimal.Parse((Rec_tmp[0x04 + 0x2D] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x2F].ToString());
                                    rm69310_gammaR23.Value = Decimal.Parse((Rec_tmp[0x04 + 0x30] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x31].ToString());
                                    rm69310_gammaR24.Value = Decimal.Parse((Rec_tmp[0x04 + 0x32] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x33].ToString());

                                    rm69310_gammaG0.Value = Decimal.Parse((Rec_tmp[0x04 + 0x34] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x35].ToString());
                                    rm69310_gammaG1.Value = Decimal.Parse((Rec_tmp[0x04 + 0x36] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x37].ToString());
                                    rm69310_gammaG2.Value = Decimal.Parse((Rec_tmp[0x04 + 0x38] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x39].ToString());
                                    rm69310_gammaG3.Value = Decimal.Parse((Rec_tmp[0x04 + 0x3A] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x3B].ToString());
                                    rm69310_gammaG4.Value = Decimal.Parse((Rec_tmp[0x04 + 0x3D] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x3F].ToString());
                                    rm69310_gammaG5.Value = Decimal.Parse((Rec_tmp[0x04 + 0x40] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x41].ToString());
                                    rm69310_gammaG6.Value = Decimal.Parse((Rec_tmp[0x04 + 0x42] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x43].ToString());
                                    rm69310_gammaG7.Value = Decimal.Parse((Rec_tmp[0x04 + 0x44] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x45].ToString());
                                    rm69310_gammaG8.Value = Decimal.Parse((Rec_tmp[0x04 + 0x46] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x47].ToString());
                                    rm69310_gammaG9.Value = Decimal.Parse((Rec_tmp[0x04 + 0x48] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x49].ToString());
                                    rm69310_gammaG10.Value = Decimal.Parse((Rec_tmp[0x04 + 0x4A] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x4B].ToString());
                                    rm69310_gammaG11.Value = Decimal.Parse((Rec_tmp[0x04 + 0x4C] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x4D].ToString());
                                    rm69310_gammaG12.Value = Decimal.Parse((Rec_tmp[0x04 + 0x4E] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x4F].ToString());
                                    rm69310_gammaG13.Value = Decimal.Parse((Rec_tmp[0x04 + 0x50] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x51].ToString());
                                    rm69310_gammaG14.Value = Decimal.Parse((Rec_tmp[0x04 + 0x52] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x53].ToString());
                                    rm69310_gammaG15.Value = Decimal.Parse((Rec_tmp[0x04 + 0x54] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x55].ToString());
                                    rm69310_gammaG16.Value = Decimal.Parse((Rec_tmp[0x04 + 0x56] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x58].ToString());
                                    rm69310_gammaG17.Value = Decimal.Parse((Rec_tmp[0x04 + 0x59] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x5A].ToString());
                                    rm69310_gammaG18.Value = Decimal.Parse((Rec_tmp[0x04 + 0x5B] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x5C].ToString());
                                    rm69310_gammaG19.Value = Decimal.Parse((Rec_tmp[0x04 + 0x5D] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x5E].ToString());
                                    rm69310_gammaG20.Value = Decimal.Parse((Rec_tmp[0x04 + 0x5F] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x60].ToString());
                                    rm69310_gammaG21.Value = Decimal.Parse((Rec_tmp[0x04 + 0x61] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x62].ToString());
                                    rm69310_gammaG22.Value = Decimal.Parse((Rec_tmp[0x04 + 0x63] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x64].ToString());
                                    rm69310_gammaG23.Value = Decimal.Parse((Rec_tmp[0x04 + 0x65] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x66].ToString());
                                    rm69310_gammaG24.Value = Decimal.Parse((Rec_tmp[0x04 + 0x67] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x68].ToString());

                                    rm69310_gammaB0.Value = Decimal.Parse((Rec_tmp[0x04 + 0x69] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x6A].ToString());
                                    rm69310_gammaB1.Value = Decimal.Parse((Rec_tmp[0x04 + 0x6B] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x6C].ToString());
                                    rm69310_gammaB2.Value = Decimal.Parse((Rec_tmp[0x04 + 0x6D] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x6E].ToString());
                                    rm69310_gammaB3.Value = Decimal.Parse((Rec_tmp[0x04 + 0x6F] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x70].ToString());
                                    rm69310_gammaB4.Value = Decimal.Parse((Rec_tmp[0x04 + 0x71] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x72].ToString());
                                    rm69310_gammaB5.Value = Decimal.Parse((Rec_tmp[0x04 + 0x73] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x74].ToString());
                                    rm69310_gammaB6.Value = Decimal.Parse((Rec_tmp[0x04 + 0x75] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x76].ToString());
                                    rm69310_gammaB7.Value = Decimal.Parse((Rec_tmp[0x04 + 0x77] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x78].ToString());
                                    rm69310_gammaB8.Value = Decimal.Parse((Rec_tmp[0x04 + 0x79] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x7A].ToString());
                                    rm69310_gammaB9.Value = Decimal.Parse((Rec_tmp[0x04 + 0x7B] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x7C].ToString());
                                    rm69310_gammaB10.Value = Decimal.Parse((Rec_tmp[0x04 + 0x7D] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x7E].ToString());
                                    rm69310_gammaB11.Value = Decimal.Parse((Rec_tmp[0x04 + 0x7F] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x80].ToString());
                                    rm69310_gammaB12.Value = Decimal.Parse((Rec_tmp[0x04 + 0x81] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x82].ToString());
                                    rm69310_gammaB13.Value = Decimal.Parse((Rec_tmp[0x04 + 0x83] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x84].ToString());
                                    rm69310_gammaB14.Value = Decimal.Parse((Rec_tmp[0x04 + 0x85] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x86].ToString());
                                    rm69310_gammaB15.Value = Decimal.Parse((Rec_tmp[0x04 + 0x87] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x88].ToString());
                                    rm69310_gammaB16.Value = Decimal.Parse((Rec_tmp[0x04 + 0x89] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x8A].ToString());
                                    rm69310_gammaB17.Value = Decimal.Parse((Rec_tmp[0x04 + 0x8B] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x8C].ToString());
                                    rm69310_gammaB18.Value = Decimal.Parse((Rec_tmp[0x04 + 0x8D] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x8E].ToString());
                                    rm69310_gammaB19.Value = Decimal.Parse((Rec_tmp[0x04 + 0x8F] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x90].ToString());
                                    rm69310_gammaB20.Value = Decimal.Parse((Rec_tmp[0x04 + 0x91] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x92].ToString());
                                    rm69310_gammaB21.Value = Decimal.Parse((Rec_tmp[0x04 + 0x93] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x94].ToString());
                                    rm69310_gammaB22.Value = Decimal.Parse((Rec_tmp[0x04 + 0x95] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x96].ToString());
                                    rm69310_gammaB23.Value = Decimal.Parse((Rec_tmp[0x04 + 0x97] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x98].ToString());
                                    rm69310_gammaB24.Value = Decimal.Parse((Rec_tmp[0x04 + 0x99] & 0x03).ToString()) * 256 + Decimal.Parse(Rec_tmp[0x04 + 0x9A].ToString());

                                    break;

                                case 0x05://///write otp ok  RM69310
                                    textBox1.Text = textBox1.Text + "OTP  \t  写 OK！\r\n";
                                    break;

                                case 0x06://///read otp ok  RM69310
                                    textBox1.Text = textBox1.Text + "OTP  \t  Read OK！\r\n";

                                    tmp = String.Format("{0:X2}", Rec_tmp[3]);//16进制
                                    switch (Rec_tmp[3] & 0xe0)
                                    {
                                        case 0x00:
                                            textBox_40.Text = "0";
                                            break;

                                        case 0x20:
                                            textBox_40.Text = "1";
                                            break;

                                        case 0x60:
                                            textBox_40.Text = "2";
                                            break;

                                        case 0xe0:
                                            textBox_40.Text = "3";
                                            break;

                                        default:
                                            textBox_40.Text = "Er";
                                            break;
                                    }

                                    switch (Rec_tmp[3] & 0x18)
                                    {
                                        case 0x00:
                                            textBox_50.Text = "0";
                                            break;

                                        case 0x10:
                                            textBox_50.Text = "1";
                                            break;

                                        case 0x18:
                                            textBox_50.Text = "2";
                                            break;

                                        default:
                                            textBox_50.Text = "Er";
                                            break;
                                    }

                                    switch (Rec_tmp[3] & 0x06)
                                    {
                                        case 0x00:
                                            textBox_60.Text = "0";
                                            break;

                                        case 0x04:
                                            textBox_60.Text = "1";
                                            break;

                                        case 0x06:
                                            textBox_60.Text = "2";
                                            break;

                                        default:
                                            textBox_60.Text = "Er";
                                            break;
                                    }

                                    switch (Rec_tmp[3] & 0x01)
                                    {
                                        case 0x00:
                                            textBox_70.Text = "0";
                                            break;

                                        case 0x01:
                                            textBox_70.Text = "1";
                                            break;

                                        default:
                                            textBox_70.Text = "Er";
                                            break;
                                    }
                                    tmp = String.Format("{0:X2}", Rec_tmp[4]);//16进制
                                    if ((Rec_tmp[4] & 0x80) == 0x80)
                                    {
                                        textBox_70.Text = "2";
                                    }

                                    break;
                            }
                        }

                        ////---------------------------------------------------Demura----------------------------------------
                        else if (Rec_tmp[0] == 0x2F)  //Demura
                        {
                            if ((Rec_tmp[7] == 0x2E) || (Rec_tmp[7] == 0x3C))   // 297/298/299  //350
                            {
                                if (Rec_tmp[7] == 0x2E)// 297/298/299
                                {
                                    textBox1.Text = textBox1.Text + "297 298 299\r\n";
                                }
                                else if (Rec_tmp[7] == 0x3C) //350
                                {
                                    textBox1.Text = textBox1.Text + "350\r\n";
                                }
                                switch (Rec_tmp[1])
                                {
                                    case 0x01:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura RAM Enable\r\n";
                                        }
                                        break;
                                    case 0x02:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura RAM Disable\r\n";
                                        }
                                        break;
                                    case 0x03:
                                        {
                                            if (Rec_tmp[7] == 0x2E)// 297/298/299
                                            {
                                                if (Rec_tmp[4] == 0x80)
                                                {
                                                    textBox1.Text = textBox1.Text + "Demura RAM write Flash complete ! \r\n";
                                                }
                                            }
                                            else if (Rec_tmp[7] == 0x3C) ///350
                                            {
                                                if (Rec_tmp[4] == 0x08)
                                                {
                                                    textBox1.Text = textBox1.Text + "Demura RAM write Flash1 complete ! \r\n";
                                                }
                                            }
                                        }
                                        break;
                                    case 0x0A:///350 only
                                        {
                                            if (Rec_tmp[7] == 0x3C) ///350
                                            {
                                                if (Rec_tmp[4] == 0x08)
                                                {
                                                    textBox1.Text = textBox1.Text + "Demura RAM write Flash2 complete ! \r\n";
                                                }
                                            }
                                        }
                                        break;
                                    case 0x04:
                                        {
                                            textBox1.Text = textBox1.Text + "Checksum: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "\r\n";
                                        }
                                        break;
                                    case 0x05:
                                        {
                                            if (Rec_tmp[7] == 0x2E)// 297/298/299
                                            {
                                                if (Rec_tmp[4] == 0x80)
                                                {
                                                    textBox1.Text = textBox1.Text + "Flash Erase Success ! \r\n";
                                                }
                                            }
                                            else if (Rec_tmp[7] == 0x3C) ///350
                                            {
                                                if (Rec_tmp[4] == 0x08)
                                                {
                                                    textBox1.Text = textBox1.Text + "Flash Erase Success ! \r\n";
                                                }
                                            }
                                        }
                                        break;
                                    case 0x06:
                                        {
                                            textBox1.Text = textBox1.Text + "flash ID: " + Rec_tmp[4].ToString("x") + "," + Rec_tmp[5].ToString("x") + "," + Rec_tmp[6].ToString("x") + "\r\n";
                                        }
                                        break;
                                    case 0x07:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura function on \r\n";
                                        }
                                        break;
                                    case 0x08:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura function off \r\n";
                                        }
                                        break;
                                    case 0x09:
                                        {
                                            textBox1.Text = textBox1.Text + "Demura OTP complete$ \r\n";
                                        }
                                        break;
                                    default:
                                        break;
                                }
                            }
                        }

                        textBox1.Focus();//获取焦点
                        textBox1.Select(textBox1.TextLength, 0);//光标定位到文本最后
                        textBox1.ScrollToCaret();//滚动到光标处
                        if (textBox1.Lines.Length > 200) textBox1.Text = "";
                    }));
                }
            }
            finally
            {
                Listening = false;

            }

        }

        //private void RM67198_rbt_Gamma_CheckedChanged(object sender, EventArgs e)
        //{
        //    RadioButton btnTmp = (RadioButton)sender;
        //    if (MainForm.IC_comboBox.SelectedIndex == 5)  //if RM67198
        //    {
        //        RM67198_gamma_point_cs.Text = "RM67198绑点切换";
        //    }
        //    else if (MainForm.IC_comboBox.SelectedIndex == 10)
        //    {
        //        RM67198_gamma_point_cs.Text = "RM69298绑点切换";
        //    }
        //    switch (btnTmp.Name)
        //    {
        //        case "RM67198_rbt_33GP":
        //            {
        //                txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "50";
        //                RM196_Others_Click(RM67195_RegW, null);
        //                Application.DoEvents();
        //                txtRM67195Add.Text = "DA"; txtRM67195Data.Text = "01";
        //                RM196_Others_Click(RM67195_RegW, null);
        //                Application.DoEvents();
        //                NoRMR1.Enabled = true; NoRMG1.Enabled = true; NoRMB1.Enabled = true;
        //                NoRMR2.Enabled = true; NoRMG2.Enabled = true; NoRMB2.Enabled = true;
        //                NoRMR3.Enabled = true; NoRMG3.Enabled = true; NoRMB3.Enabled = true;
        //                NoRMR28.Enabled = true; NoRMG28.Enabled = true; NoRMB28.Enabled = true;
        //                NoRMR44.Enabled = true; NoRMG44.Enabled = true; NoRMB44.Enabled = true;
        //                NoRMR84.Enabled = true; NoRMG84.Enabled = true; NoRMB84.Enabled = true;
        //                NoRMR116.Enabled = true; NoRMG116.Enabled = true; NoRMB116.Enabled = true;
        //                NoRMR148.Enabled = true; NoRMG148.Enabled = true; NoRMB148.Enabled = true;
        //                NoRMR180.Enabled = true; NoRMG180.Enabled = true; NoRMB180.Enabled = true;
        //                NoRMR188.Enabled = true; NoRMG188.Enabled = true; NoRMB188.Enabled = true;
        //                NoRMR204.Enabled = true; NoRMG204.Enabled = true; NoRMB204.Enabled = true;
        //                NoRMR212.Enabled = true; NoRMG212.Enabled = true; NoRMB212.Enabled = true;
        //                NoRMR220.Enabled = true; NoRMG220.Enabled = true; NoRMB220.Enabled = true;
        //                NoRMR236.Enabled = true; NoRMG236.Enabled = true; NoRMB236.Enabled = true;
        //                NoRMR248.Enabled = true; NoRMG248.Enabled = true; NoRMB248.Enabled = true;

        //            }
        //            break;
        //        case "RM67198_rbt_31GP":
        //            {
        //                txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "50";
        //                RM196_Others_Click(RM67195_RegW, null);
        //                Application.DoEvents();
        //                txtRM67195Add.Text = "DA"; txtRM67195Data.Text = "00";
        //                RM196_Others_Click(RM67195_RegW, null);
        //                Application.DoEvents();
        //                NoRMR1.Enabled = false; NoRMG1.Enabled = false; NoRMB1.Enabled = false;
        //                NoRMR2.Enabled = false; NoRMG2.Enabled = false; NoRMB2.Enabled = false;
        //                NoRMR3.Enabled = true; NoRMG3.Enabled = true; NoRMB3.Enabled = true;
        //                NoRMR28.Enabled = true; NoRMG28.Enabled = true; NoRMB28.Enabled = true;
        //                NoRMR44.Enabled = true; NoRMG44.Enabled = true; NoRMB44.Enabled = true;
        //                NoRMR84.Enabled = true; NoRMG84.Enabled = true; NoRMB84.Enabled = true;
        //                NoRMR116.Enabled = true; NoRMG116.Enabled = true; NoRMB116.Enabled = true;
        //                NoRMR148.Enabled = true; NoRMG148.Enabled = true; NoRMB148.Enabled = true;
        //                NoRMR180.Enabled = true; NoRMG180.Enabled = true; NoRMB180.Enabled = true;
        //                NoRMR188.Enabled = true; NoRMG188.Enabled = true; NoRMB188.Enabled = true;
        //                NoRMR204.Enabled = true; NoRMG204.Enabled = true; NoRMB204.Enabled = true;
        //                NoRMR212.Enabled = true; NoRMG212.Enabled = true; NoRMB212.Enabled = true;
        //                NoRMR220.Enabled = true; NoRMG220.Enabled = true; NoRMB220.Enabled = true;
        //                NoRMR236.Enabled = true; NoRMG236.Enabled = true; NoRMB236.Enabled = true;
        //                NoRMR248.Enabled = true; NoRMG248.Enabled = true; NoRMB248.Enabled = true;

        //            }
        //            break;
        //        case "RM67198_rbt_19GP":
        //            {
        //                txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "50";
        //                RM196_Others_Click(RM67195_RegW, null);
        //                Application.DoEvents();
        //                txtRM67195Add.Text = "DA"; txtRM67195Data.Text = "03";
        //                RM196_Others_Click(RM67195_RegW, null);
        //                Application.DoEvents();

        //                NoRMR1.Enabled = false; NoRMG1.Enabled = false; NoRMB1.Enabled = false;
        //                NoRMR2.Enabled = true; NoRMG2.Enabled = true; NoRMB2.Enabled = true;
        //                NoRMR3.Enabled = false; NoRMG3.Enabled = false; NoRMB3.Enabled = false;
        //                NoRMR28.Enabled = false; NoRMG28.Enabled = false; NoRMB28.Enabled = false;
        //                NoRMR44.Enabled = false; NoRMG44.Enabled = false; NoRMB44.Enabled = false;
        //                NoRMR84.Enabled = false; NoRMG84.Enabled = false; NoRMB84.Enabled = false;
        //                NoRMR116.Enabled = false; NoRMG116.Enabled = false; NoRMB116.Enabled = false;
        //                NoRMR148.Enabled = false; NoRMG148.Enabled = false; NoRMB148.Enabled = false;
        //                NoRMR180.Enabled = false; NoRMG180.Enabled = false; NoRMB180.Enabled = false;
        //                NoRMR188.Enabled = false; NoRMG188.Enabled = false; NoRMB188.Enabled = false;
        //                NoRMR204.Enabled = false; NoRMG204.Enabled = false; NoRMB204.Enabled = false;
        //                NoRMR212.Enabled = false; NoRMG212.Enabled = false; NoRMB212.Enabled = false;
        //                NoRMR220.Enabled = false; NoRMG220.Enabled = false; NoRMB220.Enabled = false;
        //                NoRMR236.Enabled = false; NoRMG236.Enabled = false; NoRMB236.Enabled = false;
        //                NoRMR248.Enabled = false; NoRMG248.Enabled = false; NoRMB248.Enabled = false;
        //            }
        //            break;
        //        case "RM67198_rbt_NorM":
        //            {
        //                txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "40";
        //                RM196_Others_Click(RM67195_RegW, null);
        //                Application.DoEvents();
        //                txtRM67195Add.Text = "E6"; txtRM67195Data.Text = "10";
        //                RM196_Others_Click(RM67195_RegW, null);
        //                Application.DoEvents();
        //                txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "00";
        //                RM196_Others_Click(RM67195_RegW, null);
        //                Application.DoEvents();
        //                txtRM67195Add.Text = "53"; txtRM67195Data.Text = "20";
        //                RM196_Others_Click(RM67195_RegW, null);
        //                Application.DoEvents();
        //                txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "50";
        //                RM196_Others_Click(RM67195_RegW, null);
        //                Application.DoEvents();
        //            }
        //            break;
        //        case "RM67198_rbt_HBM":
        //            {
        //                if (MainForm.IC_comboBox.SelectedIndex == 5)  //if RM67198
        //                {
        //                    txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "40";
        //                    RM196_Others_Click(RM67195_RegW, null);
        //                    Application.DoEvents();
        //                    txtRM67195Add.Text = "E6"; txtRM67195Data.Text = "00";
        //                    RM196_Others_Click(RM67195_RegW, null);
        //                    Application.DoEvents();
        //                    txtRM67195Add.Text = "64"; txtRM67195Data.Text = "02";
        //                    RM196_Others_Click(RM67195_RegW, null);
        //                    Application.DoEvents();
        //                    txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "00";
        //                    RM196_Others_Click(RM67195_RegW, null);
        //                    Application.DoEvents();
        //                    txtRM67195Add.Text = "53"; txtRM67195Data.Text = "E0";
        //                    RM196_Others_Click(RM67195_RegW, null);
        //                    Application.DoEvents();
        //                    txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "51";
        //                    RM196_Others_Click(RM67195_RegW, null);
        //                    Application.DoEvents();
        //                }
        //                else if (MainForm.IC_comboBox.SelectedIndex == 10)  //if RM69298
        //                {
        //                    txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "40";
        //                    RM196_Others_Click(RM67195_RegW, null);
        //                    Application.DoEvents();
        //                    txtRM67195Add.Text = "E6"; txtRM67195Data.Text = "00";
        //                    RM196_Others_Click(RM67195_RegW, null);
        //                    Application.DoEvents();
        //                    txtRM67195Add.Text = "64"; txtRM67195Data.Text = "02";
        //                    RM196_Others_Click(RM67195_RegW, null);
        //                    Application.DoEvents();
        //                    txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "00";
        //                    RM196_Others_Click(RM67195_RegW, null);
        //                    Application.DoEvents();
        //                    txtRM67195Add.Text = "53"; txtRM67195Data.Text = "E0";
        //                    RM196_Others_Click(RM67195_RegW, null);
        //                    Application.DoEvents();
        //                    txtRM67195Add.Text = "fe"; txtRM67195Data.Text = "52";
        //                    RM196_Others_Click(RM67195_RegW, null);
        //                    Application.DoEvents();
        //                }
        //            }
        //            break;
        //        default: break;
        //    }
        //}

        //private void LCD_Cfg_Click(object sender, EventArgs e)
        //{
        //    MainForm.LCD_Cfg_Click( sender,  e);
        //}
        //=================================LCD cfg function:OLED_Show_Click====Start=================
        private void OLED_Show_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[25];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 25;
            UInt32 ICS307Temp = 0x00;
            Send_buf[0] = 0x0A; //LCD配置标志
            Send_buf[2] = 0x15; //数据长度
            switch (btnTmp.Name)
            {

                case "btnPatternW":                             //写入Pattern参数
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = byte.Parse(numPatternR.Value.ToString());
                        Send_buf[4] = byte.Parse(numPatternG.Value.ToString());
                        Send_buf[5] = byte.Parse(numPatternB.Value.ToString());
                    }
                    break;

                case "btnred":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break; //R
                case "btngreen":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0;
                    }
                    break; //G
                case "btnblue":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0xFF;
                    }
                    break; //B
                case "btnwhite":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0xFF;
                        Send_buf[4] = 0xFF;
                        Send_buf[5] = 0xFF;
                    }
                    break; //W
                case "btnblack":
                    {
                        Send_buf[1] = 0x07;
                        Send_buf[3] = 0;
                        Send_buf[4] = 0;
                        Send_buf[5] = 0;
                    }
                    break; //BL
                default:
                    break;
            }

            CRCData.CalculateCrc16(Send_buf, out Send_buf[23], out Send_buf[24]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 25);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            Thread.Sleep(800);
        }

        private void numPatternR_ValueChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternG.Value;
            }
        }

        private void checkBox1_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox1.Checked == true)
            {
                numPatternG.Value = numPatternR.Value;
                numPatternB.Value = numPatternG.Value;
            }
        }


        //------------------------------------------Demura------------------------------------------------
        private void Demura_Click(object sender, EventArgs e)
        {

            if (MainForm.IC_comboBox.SelectedIndex == 13)  //是新思IC
            {
                Button btnTmp = (Button)sender;
                switch (btnTmp.Name)
                {
                    case "Demura_Function_ON":
                        {
                            try
                            {
                                MainForm.comPort.Write(SyR66455_621_DeMura_ON_Part_0, 0, 7);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }

                            T1.Enabled = false;
                            T1.Interval = 50;
                            T1.Enabled = true;
                            try
                            {
                                MainForm.comPort.Write(SyR66455_621_DeMura_ON_Part_1, 0, 65);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }

                            T1.Enabled = false;
                            T1.Interval = 100;
                            T1.Enabled = true;
                        }
                        break;
                    case "Demura_Function_OFF":
                        {
                            try
                            {
                                MainForm.comPort.Write(SyR66455_621_DeMura_OFF_Part_0, 0, 7);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }

                            T1.Enabled = false;
                            T1.Interval = 50;
                            T1.Enabled = true;
                            try
                            {
                                MainForm.comPort.Write(SyR66455_621_DeMura_OFF_Part_1, 0, 65);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }

                            T1.Enabled = false;
                            T1.Interval = 100;
                            T1.Enabled = true;
                        }
                        break;
                    case "Demura_Flash_Enable":
                        {
                            try
                            {
                                Setting.TempRecord.cmdLen = 7;
                                MainForm.comPort.Write(Test1, 0, 7);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }


                            try
                            {
                                Setting.TempRecord.cmdLen = 56;
                                MainForm.comPort.Write(Test2, 0, 56);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }



                            try
                            {
                                Setting.TempRecord.cmdLen = 11;
                                MainForm.comPort.Write(Test3, 0, 11);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }



                            try
                            {
                                Setting.TempRecord.cmdLen = 7;
                                MainForm.comPort.Write(Test4, 0, 7);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }



                            try
                            {
                                Setting.TempRecord.cmdLen = 7;
                                MainForm.comPort.Write(Test5, 0, 7);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }



                            try
                            {
                                Setting.TempRecord.cmdLen = 7;
                                MainForm.comPort.Write(Test6, 0, 7);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }
                            Thread.Sleep(50);


                            try
                            {
                                Setting.TempRecord.cmdLen = 9;
                                MainForm.comPort.Write(Test7, 0, 9);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }




                        }
                        break;

                    case "Flash_Check_Demura_data":
                        {
                            //try
                            //{
                            //    Setting.TempRecord.cmdLen = 7;
                            //    MainForm.comPort.Write(SyR66455_Read_DeMura_CheckSum_0, 0, 7);
                            //}
                            //catch
                            //{
                            //    MessageBox.Show("串口有误，请检查", "错误");
                            //}
                            //try
                            //{
                            //    Setting.TempRecord.cmdLen = 6;
                            //    MainForm.comPort.Write(SyR66455_Read_DeMura_CheckSum_1, 0, 6);
                            //}
                            //catch
                            //{
                            //    MessageBox.Show("串口有误，请检查", "错误");
                            //}
                            try
                            {
                                Setting.TempRecord.cmdLen = 9;
                                MainForm.comPort.Write(SyR66455_Read_DeMura_CheckSum_2, 0, 9);
                            }
                            catch
                            {
                                MessageBox.Show("串口有误，请检查", "错误");
                            }
                        }
                        break;
                    case "Flash_Write_Demura_data":
                        {

                        }
                        break;



                    default: break;





                }
            }
            else//不是新思IC
            {
                //string[] RM67298_Enable_Flash_Control = new string[4] { "fe", "20", "f3", "02"};
                Button btnTmp = (Button)sender;
                Send_buf = new byte[10];
                Setting.TempRecord.cmdLen = 10;
                Send_buf[0] = 0x2F;
                Send_buf[2] = 0x05;
                if (MainForm.IC_comboBox.SelectedIndex == 10)  //if RM297/298/299
                {
                    Send_buf[7] = 0x2E;
                }
                else if (MainForm.IC_comboBox.SelectedIndex == 11)   //if RM350
                {
                    Send_buf[7] = 0x3C;
                }
                switch (btnTmp.Name)
                {
                    case "Demura_Function_ON":
                        {
                            Send_buf[1] = 0x07;
                        }
                        break;
                    case "Demura_Function_OFF":
                        {
                            Send_buf[1] = 0x08;
                        }
                        break;
                    case "Inter_Demura_data":
                        {
                            Send_buf[1] = 0x01;
                        }
                        break;
                    case "Exit_Demura_data":
                        {
                            Send_buf[1] = 0x02;
                        }
                        break;
                    case "Flash_Write_Demura_data":
                        {
                            Send_buf[1] = 0x03;
                        }
                        break;
                    case "Flash_Check_Demura_data":
                        {
                            Send_buf[1] = 0x04;
                        }
                        break;
                    case "Flash_Erase_Demura_data":
                        {
                            Send_buf[1] = 0x05;
                        }
                        break;
                    case "Demura_Flash_Enable":
                        {
                            Send_buf[1] = 0x06;
                        }
                        break;
                    case "Flash_Demura_OTP":
                        {
                            Send_buf[1] = 0x09;
                        }
                        break;
                    case "Flash_Write_Demura_data2":
                        {
                            Send_buf[1] = 0x0A;
                        }
                        break;
                    default:
                        break;
                }
                CRCData.CalculateCrc16(Send_buf, out Send_buf[8], out Send_buf[9]);


                try
                {
                    MainForm.comPort.Write(Send_buf, 0, 10);
                }
                catch
                {
                    MessageBox.Show("串口有误，请检查", "错误");
                }
            }




        }

        private void btnReload_Demura_File_Click(object sender, EventArgs e)
        {
            //FolderBrowserDialog fldb = new FolderBrowserDialog();
            //fldb.ShowNewFolderButton = false;
            //DialogResult res = fldb.ShowDialog();
            //if (res != System.Windows.Forms.DialogResult.OK)
            //{
            //    MessageBox.Show("没选择任何文件.");
            //    return;
            //}
            //string path = fldb.SelectedPath;

            //if (!Directory.Exists(path))
            //{
            //    MessageBox.Show("文件夹不存在.");
            //    return;
            //}
            //listBox3_Demura_File.Items.Clear();
            //string s = null;
            //DirectoryInfo d = new DirectoryInfo(path);
            //FileInfo[] Files = d.GetFiles("*.hex");

            //// List<string> lstr = new List<string>();

            //foreach (FileInfo file in Files)
            //{
            //    s = file.Name;
            //    listBox3_Demura_File.Items.Add(s);
            //}

            ////zangqiang 20180522  增加bin文件 识别
            //FileInfo[] Files1 = d.GetFiles("*.bin");
            //foreach (FileInfo file in Files1)
            //{
            //    s = file.Name;
            //    listBox3_Demura_File.Items.Add(s);
            //}
            ////zangqiang 20180522  增加bin文件 识别

            //textBox5.Text = path;
        }

        private void Load_Demura_data_Click(object sender, EventArgs e)
        {
            //Int32 m_txlen = 0;
            //Int32 Once_txlen = 0;
            //Int32 File_Length, File_Length_tep;
            //bool tx_en = true;
            //byte[] TemFileContent = new byte[1024 * 100];
            //char[] Acsii_array = new char[2];
            //Int32 div = 0;
            //div = Convert.ToInt16(MainForm.HACT.Value) * 3;
            ////div = Convert.ToInt16(LCD_X_SIZE) * 3;
            ////div = 1080*3;
            ////div = 255;
            //System.Int32 ByteReaded = 0x00;
            //try
            //{
            //    string FileName = textBox5.Text + '\\' + listBox3_Demura_File.SelectedItem.ToString();
            //    FileStream aFile = new FileStream(FileName, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
            //    File_Length = Convert.ToInt32(aFile.Length);
            //    File_Length_tep = File_Length;
            //    while (tx_en)
            //    {
            //        if (File_Length_tep > (File_Length % div))
            //        {
            //            Once_txlen = div;
            //        }
            //        else  //最后一笔
            //        {
            //            Once_txlen = Convert.ToInt32(aFile.Length % div);
            //            tx_en = false;
            //        }
            //        aFile.Seek(0 + m_txlen, SeekOrigin.Begin);    //跳转
            //        ByteReaded = aFile.Read(TemFileContent, 5, Once_txlen); // 存入第五位
            //        try
            //        {
            //            TemFileContent[0] = 0x5B;
            //            TemFileContent[1] = Convert.ToByte(File_Length >> 24);
            //            TemFileContent[2] = Convert.ToByte((File_Length >> 16) & 0xff);
            //            TemFileContent[3] = Convert.ToByte((File_Length >> 8) & 0xff);
            //            TemFileContent[4] = Convert.ToByte(File_Length & 0xff);
            //            MainForm.comPort.Write(TemFileContent, 0, ByteReaded + 5);
            //        }
            //        catch
            //        {
            //            MessageBox.Show("串口有误，请检查", "错误");
            //            tx_en = false;
            //            break;
            //        }
            //        //tx_en = false;
            //        m_txlen += ByteReaded;
            //        progressBar2.Value = Convert.ToInt32((double)m_txlen / File_Length * 100);
            //        label_flash.Text = progressBar2.Value.ToString() + "%";
            //        Application.DoEvents();//重点，必须加上，否则父子窗体都假死
            //        File_Length_tep = File_Length_tep - div;
            //    }
            //    Demura_Click(Flash_Write_Demura_data, null);

            //    //------------------------------载入第二笔数据-----------------------
            //    if (MainForm.IC_comboBox.SelectedIndex == Setting.IC_Type.RM69350) //瑞鼎（69350）
            //    {
            //        Int32 RM6935x_Demura_part2_addr = 0x180000; //第二笔补偿数据起始地址
            //        Int32 RM6935x_Demura_part2_length = (File_Length - 0x180000) + (0xF - (File_Length - 0x180000) % 0x10); //0x12310F;//第二笔补偿数据总长，256取整
            //        File_Length_tep = RM6935x_Demura_part2_length;
            //        File_Length = RM6935x_Demura_part2_length;
            //        tx_en = true;
            //        m_txlen = 0;
            //        while (tx_en)
            //        {
            //            if (File_Length_tep > (File_Length % div))
            //            {
            //                Once_txlen = div;
            //            }
            //            else  //最后一笔
            //            {
            //                Once_txlen = Convert.ToInt32(File_Length % div);
            //                tx_en = false;
            //            }
            //            aFile.Seek(RM6935x_Demura_part2_addr + m_txlen, SeekOrigin.Begin);    //跳转
            //            ByteReaded = aFile.Read(TemFileContent, 5, Once_txlen); // 存入第五位
            //            try
            //            {
            //                TemFileContent[0] = 0x5B;
            //                TemFileContent[1] = Convert.ToByte(File_Length >> 24);
            //                TemFileContent[2] = Convert.ToByte((File_Length >> 16) & 0xff);
            //                TemFileContent[3] = Convert.ToByte((File_Length >> 8) & 0xff);
            //                TemFileContent[4] = Convert.ToByte(File_Length & 0xff);
            //                MainForm.comPort.Write(TemFileContent, 0, Once_txlen + 5);
            //            }
            //            catch
            //            {
            //                MessageBox.Show("串口有误，请检查", "错误");
            //                tx_en = false;
            //                break;
            //            }
            //            //tx_en = false;
            //            m_txlen += ByteReaded;
            //            progressBar2.Value = Convert.ToInt32((double)m_txlen / File_Length * 100);
            //            label_flash.Text = progressBar2.Value.ToString() + "%";
            //            Application.DoEvents();
            //            File_Length_tep = File_Length_tep - div;
            //        }
            //        Demura_Click(Flash_Write_Demura_data2, null);
            //    }
            //    else
            //    {
            //        MessageBox.Show("载入完成！");
            //    }
            //    aFile.Close();
            //}
            //catch
            //{
            //    MessageBox.Show("请选择文件夹", "错误");
            //}

        }


        private int[] Parttern_RM69310 = new int[25] { 255, 239, 223, 207, 191, 175, 159, 143, 127, 111, 95, 79, 63, 55, 47, 39, 35, 31, 27, 23, 19, 15, 11, 7, 0 };
        private int[] Parttern_RM67195 = new int[30] { 255, 254, 252, 248, 244, 236, 228, 220, 212, 204, 196, 188, 180, 164, 148, 132, 116, 100, 84, 68, 52, 44, 36, 28, 20, 16, 12, 8, 4, 3 };
        private void CAS200_Measure_Information(double Dgv_lum, double Dgv_x, double Dgv_y)
        {
            int Row_Accum = 0, Current_Index = 0, Row_flag = 0;

            DataGridViewRow Row1 = new DataGridViewRow();
            Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
            if (Row_Accum != 0)
            {
                Current_Index = this.CAS200_dataGridView.SelectedCells[0].RowIndex;
                if ((Current_Index != Row_Accum - 1) && (Current_Index != Row_Accum))
                {
                    Row_Accum = Current_Index;
                    Row_flag = 1;
                }
                else
                {
                    this.CAS200_dataGridView.Rows.Add(Row1);
                }
                if ((Row_Accum != 0) && (Row_flag != 1))
                {
                    if (Current_Index != Row_Accum)
                    {
                        this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = false;
                        this.CAS200_dataGridView.Rows[Row_Accum].Selected = true;
                    }
                }
            }
            else
            {
                this.CAS200_dataGridView.Rows.Add(Row1);
            }
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[0].Value = Row_Accum + 1;//序号
            //Rows[x]为第x行,Cells[x]为第x列
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[1].Value = Dgv_x.ToString("#0.0000");
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[2].Value = Dgv_y.ToString("#0.0000");
            this.CAS200_dataGridView.Rows[Row_Accum].Cells[3].Value = Dgv_lum.ToString("#0.0000");
            if (Row_flag == 1)
            {
                Row_flag = 0;
                Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
                this.CAS200_dataGridView.Rows[Current_Index].Selected = false;
                this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = true;
            }
            CAS200_dataGridView.FirstDisplayedScrollingRowIndex = CAS200_dataGridView.RowCount - 1;
        }
        private void Pattern_Write(byte R_value, byte G_value, byte B_value)
        {
            numPatternR.Value = R_value;
            numPatternG.Value = G_value;
            numPatternB.Value = B_value;
            OLED_Show_Click(btnPatternW, null);
        }
        private void T3_Tick(object sender, EventArgs e)
        {
            //    if (check_follow_CE_Auto_measure.Checked == true)
            //    {
            //        string strLine = Convert.ToString(text_CE_data.Text);
            //        string[] arr = strLine.Split(','); //以逗号分割，添加到数组              
            //        if ((MainForm.IC_comboBox.SelectedIndex == 0) || (MainForm.IC_comboBox.SelectedIndex == 4) || (MainForm.IC_comboBox.SelectedIndex == 5) || (MainForm.IC_comboBox.SelectedIndex == 10)) //rm67195
            //        {
            //            txtRM67195Add.Text = arr[0];
            //            txtRM67195Data.Text = CA200_Measure_CE.ToString("x");
            //            RM196_Others_Click(RM67195_RegW, e);
            //            Thread.Sleep(20);
            //        }
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 6) //NT37710
            //        //{
            //        //    /*
            //        //    Setting.TempRecord.cmdLen = arr.Length + 5;
            //        //    Send_buf = new byte[arr.Length + 5];
            //        //    Send_buf[0] = 0x2A;
            //        //    Send_buf[1] = 0x01;
            //        //    Send_buf[2] = byte.Parse(arr.Length.ToString());
            //        //    for (int i = 0; i < arr.Length-1; i++)
            //        //    {
            //        //        Send_buf[3 + i] = byte.Parse((Convert.ToInt32(arr[i], 16)).ToString());
            //        //    }
            //        //    arr[arr.Length - 1] = CA200_Measure_CE.ToString("x");
            //        //    CRCData.CalculateCrc16(Send_buf, out Send_buf[5 + arr.Length - 2], out Send_buf[5 + arr.Length - 1]);
            //        //    try
            //        //    {
            //        //        comPort.Write(Send_buf, 0, 5 + arr.Length);
            //        //    }
            //        //    catch
            //        //    {
            //        //        MessageBox.Show("串口有误，请检查", "错误");
            //        //    } */
            //        //    NT37710_txtRegData.Text = "";
            //        //    for (int i = 0; i < arr.Length - 1; i++)
            //        //    {
            //        //        NT37710_txtRegData.Text += arr[i].ToString() + ",";
            //        //    }
            //        //    NT37710_txtRegData.Text += CA200_Measure_CE.ToString("x");
            //        //    NT37710_RegWR_Click(NT37710_RegW, e);
            //        //    // T1.Enabled = false;
            //        //    // T1.Interval = 2000;
            //        //    //T1.Enabled = true;
            //        //    Thread.Sleep(20);
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 8)//69300
            //        //{
            //        //    RM69300_Reg_Addr.Text = arr[0];
            //        //    RM69300_Reg_Data.Text = CA200_Measure_CE.ToString("x");
            //        //    RM300_Others_Click(RM69300_Reg_W, e);
            //        //    Thread.Sleep(20);
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 9)//ICN9608
            //        //{
            //        //    ICN9608_txtRegData.Text = text_CE_data.Text;
            //        //    ICN9608_RegWR_Click(ICN9608_RegW, e);
            //        //    Thread.Sleep(20);
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 11)//69350
            //        //{
            //        //    R69350_Reg_Addr.Text = arr[0];
            //        //    R69350_Reg_Data.Text = CA200_Measure_CE.ToString("x");
            //        //    RM350_Others_Click(RM69350_Reg_W, e);
            //        //    Thread.Sleep(20);
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 15)//CH13721
            //        //{
            //        //    CH13721_txtRegData.Text = text_CE_data.Text;
            //        //    CH13721_RegWR_Click(CH13721_RegW, e);
            //        //    Thread.Sleep(20);
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 16)//UD61720
            //        //{
            //        //    UD61720_txtRegData.Text = text_CE_data.Text;
            //        //    UD61720_RegWR_Click(UD61720_RegW, e);
            //        //    Thread.Sleep(20);
            //        //}
            //        CA200_Measure_CE = CA200_Measure_CE + int.Parse(CAS200_txt0.Text);

            //        if (CA200_Measure_CE > (byte.Parse((Convert.ToInt32(arr[arr.Length - 1], 16).ToString())) + Convert.ToInt32(text_CE_times.Text) - 1))
            //            T3.Enabled = false;
            //    }

            //    else if (cbx_follow_51_points.Checked == true)
            //    {
            //        if ((MainForm.IC_comboBox.SelectedIndex == 0) || (MainForm.IC_comboBox.SelectedIndex == 4) || (MainForm.IC_comboBox.SelectedIndex == 5) || (MainForm.IC_comboBox.SelectedIndex == 10)) //rm67195
            //        {
            //            txtRM67195Add.Text = "FE";
            //            txtRM67195Data.Text = "00";
            //            RM196_Others_Click(RM67195_RegW, e);

            //            txtRM67195Add.Text = "51";
            //            txtRM67195Data.Text = CAS200_Measure_Gray.ToString("x");
            //            RM196_Others_Click(RM67195_RegW, e);
            //        }
            //        //if (MainForm.IC_comboBox.SelectedIndex == 7) //HX83200
            //        //{
            //        //    txtHX83200Add.Text = "FE";
            //        //    txtHX83200Data.Text = "00";
            //        //    HX83200_RegWR_Click(HX83200_RegW, e);

            //        //    txtHX83200Add.Text = "51";
            //        //    txtHX83200Data.Text = CAS200_Measure_Gray.ToString("x");
            //        //    HX83200_RegWR_Click(HX83200_RegW, e);
            //        //}

            //        //else if ((MainForm.IC_comboBox.SelectedIndex == 2) || (MainForm.IC_comboBox.SelectedIndex == 3)) //rm67160/62
            //        //{
            //        //    txtRM67160Add.Text = "FE";
            //        //    txtRM67160Data.Text = "00";
            //        //    btnModeSet_Click(btnRegW, e);

            //        //    txtRM67160Add.Text = "51";
            //        //    CAS200_Measure_Gray.ToString("x");
            //        //    btnModeSet_Click(btnRegW, e);
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 8)//69300
            //        //{
            //        //    RM69300_Reg_Addr.Text = "FE";
            //        //    RM69300_Reg_Data.Text = "00";
            //        //    RM300_Others_Click(RM69300_51Reg_W, e);

            //        //    RM69300_Reg_Addr.Text = "51";
            //        //    RM69300_Reg_Data.Text = CAS200_Measure_Gray.ToString("x");
            //        //    RM300_Others_Click(RM69300_51Reg_W, e);
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 9)//ICN9608
            //        //{
            //        //    ICN9608_txtRegData.Text = "51," + CAS200_Measure_Gray.ToString("x");
            //        //    ICN9608_RegWR_Click(ICN9608_51RegW, e);
            //        //    Thread.Sleep(20);
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 15)//CH13721
            //        //{
            //        //    CH13721_txtRegData.Text = "51," + CAS200_Measure_Gray.ToString("x");
            //        //    CH13721_RegWR_Click(CH13721_51Reg_W, e);
            //        //    Thread.Sleep(20);
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 16)//UD61720
            //        //{
            //        //    UD61720_txtRegData.Text = "51," + CAS200_Measure_Gray.ToString("x");
            //        //    UD61720_RegWR_Click(UD61720_51Reg_W, e);
            //        //    Thread.Sleep(20);
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 11)//69350
            //        //{
            //        //    R69350_Reg_Addr.Text = "FE";
            //        //    R69350_Reg_Data.Text = "00";
            //        //    RM350_Others_Click(RM69350_51Reg_W, e);

            //        //    R69350_Reg_Addr.Text = "51";
            //        //    R69350_Reg_Data.Text = CAS200_Measure_Gray.ToString("x");
            //        //    RM350_Others_Click(RM69350_51Reg_W, e);
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 13)//66455
            //        //{
            //        //    int R66455_51h_register = 0;
            //        //    R66455_51h_register = Convert.ToInt32(4095 - (float)((float)((float)(255 - CAS200_Measure_Gray) / 255) * 4095));
            //        //    R66455_txtRegData.Text = "51," + R66455_51h_register.ToString("x4").Substring(0, 2) + "," + R66455_51h_register.ToString("x4").Substring(2, 2);
            //        //    R66455_GammaW_Click(R66455_RegW, e);
            //        //    Thread.Sleep(20);
            //        //}
            //        CAS200_Measure_Gray = CAS200_Measure_Gray - Convert.ToInt32(CAS200_txt0.Text);
            //        if (CAS200_Measure_Gray < 0)
            //            T3.Enabled = false;
            //    }
            //    else if (cbx_follow_gamma_points.Checked == true)  //follow gamma绑点
            //    {
            //        CAS200_Measure_Count++; //跳阶
            //        if ((MainForm.IC_comboBox.SelectedIndex == 0) || (MainForm.IC_comboBox.SelectedIndex == 4) || (MainForm.IC_comboBox.SelectedIndex == 5) || (MainForm.IC_comboBox.SelectedIndex == 10)) //rm67195
            //        {
            //            if (CAS200_Measure_Count < Parttern_RM67195.Length)
            //            {
            //                if (numPatternR.Value * numPatternG.Value * numPatternB.Value == 0)
            //                {
            //                    if (numPatternR.Value != 0)
            //                    {
            //                        numPatternR.Value = Parttern_RM67195[CAS200_Measure_Count];
            //                    }
            //                    if (numPatternG.Value != 0)
            //                    {
            //                        numPatternG.Value = Parttern_RM67195[CAS200_Measure_Count];
            //                    }
            //                    if (numPatternB.Value != 0)
            //                    {
            //                        numPatternB.Value = Parttern_RM67195[CAS200_Measure_Count];
            //                    }
            //                }
            //                else
            //                {
            //                    numPatternB.Value = numPatternG.Value = numPatternR.Value = Parttern_RM67195[CAS200_Measure_Count];
            //                }
            //                Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
            //            }
            //            else
            //            {
            //                CAS200_Measure_Count = 0;
            //                T3.Enabled = false;
            //            }
            //        }
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 9)  //ICN9608
            //        //{
            //        //    if (CAS200_Measure_Count < Parttern_ICN9608.Length)
            //        //    {
            //        //        if (numPatternR.Value * numPatternG.Value * numPatternB.Value == 0)
            //        //        {
            //        //            if (numPatternR.Value != 0)
            //        //            {
            //        //                numPatternR.Value = Parttern_ICN9608[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternG.Value != 0)
            //        //            {
            //        //                numPatternG.Value = Parttern_ICN9608[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternB.Value != 0)
            //        //            {
            //        //                numPatternB.Value = Parttern_ICN9608[CAS200_Measure_Count];
            //        //            }
            //        //        }
            //        //        else
            //        //        {
            //        //            numPatternB.Value = numPatternG.Value = numPatternR.Value = Parttern_ICN9608[CAS200_Measure_Count];
            //        //        }
            //        //        Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
            //        //    }
            //        //    else
            //        //    {
            //        //        CAS200_Measure_Count = 0;
            //        //        T3.Enabled = false;
            //        //    }
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 16)  //UD61720
            //        //{
            //        //    if (CAS200_Measure_Count < Parttern_UD61720.Length)
            //        //    {
            //        //        if (numPatternR.Value * numPatternG.Value * numPatternB.Value == 0)
            //        //        {
            //        //            if (numPatternR.Value != 0)
            //        //            {
            //        //                numPatternR.Value = Parttern_UD61720[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternG.Value != 0)
            //        //            {
            //        //                numPatternG.Value = Parttern_UD61720[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternB.Value != 0)
            //        //            {
            //        //                numPatternB.Value = Parttern_UD61720[CAS200_Measure_Count];
            //        //            }
            //        //        }
            //        //        else
            //        //        {
            //        //            numPatternB.Value = numPatternG.Value = numPatternR.Value = Parttern_UD61720[CAS200_Measure_Count];
            //        //        }
            //        //        Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
            //        //    }
            //        //    else
            //        //    {
            //        //        CAS200_Measure_Count = 0;
            //        //        T3.Enabled = false;
            //        //    }
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 15)  //CH13721
            //        //{
            //        //    if (CAS200_Measure_Count < Parttern_CH13721.Length)
            //        //    {
            //        //        if (numPatternR.Value * numPatternG.Value * numPatternB.Value == 0)
            //        //        {
            //        //            if (numPatternR.Value != 0)
            //        //            {
            //        //                numPatternR.Value = Parttern_CH13721[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternG.Value != 0)
            //        //            {
            //        //                numPatternG.Value = Parttern_CH13721[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternB.Value != 0)
            //        //            {
            //        //                numPatternB.Value = Parttern_CH13721[CAS200_Measure_Count];
            //        //            }
            //        //        }
            //        //        else
            //        //        {
            //        //            numPatternB.Value = numPatternG.Value = numPatternR.Value = Parttern_CH13721[CAS200_Measure_Count];
            //        //        }
            //        //        Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
            //        //    }
            //        //    else
            //        //    {
            //        //        CAS200_Measure_Count = 0;
            //        //        T3.Enabled = false;
            //        //    }
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 8)  //rm69300
            //        //{
            //        //    if (CAS200_Measure_Count < Parttern_RM67300.Length)
            //        //    {
            //        //        if (numPatternR.Value * numPatternG.Value * numPatternB.Value == 0)
            //        //        {
            //        //            if (numPatternR.Value != 0)
            //        //            {
            //        //                numPatternR.Value = Parttern_RM67300[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternG.Value != 0)
            //        //            {
            //        //                numPatternG.Value = Parttern_RM67300[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternB.Value != 0)
            //        //            {
            //        //                numPatternB.Value = Parttern_RM67300[CAS200_Measure_Count];
            //        //            }
            //        //        }

            //        //        else
            //        //        {
            //        //            numPatternB.Value = numPatternG.Value = numPatternR.Value = Parttern_RM67300[CAS200_Measure_Count];
            //        //        }
            //        //        Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
            //        //    }
            //        //    else
            //        //    {
            //        //        CAS200_Measure_Count = 0;
            //        //        T3.Enabled = false;
            //        //    }
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 11)  //rm69350
            //        //{
            //        //    if (CAS200_Measure_Count < Parttern_RM67350.Length)
            //        //    {
            //        //        if (numPatternR.Value * numPatternG.Value * numPatternB.Value == 0)
            //        //        {
            //        //            if (numPatternR.Value != 0)
            //        //            {
            //        //                numPatternR.Value = Parttern_RM67350[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternG.Value != 0)
            //        //            {
            //        //                numPatternG.Value = Parttern_RM67350[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternB.Value != 0)
            //        //            {
            //        //                numPatternB.Value = Parttern_RM67350[CAS200_Measure_Count];
            //        //            }
            //        //        }

            //        //        else
            //        //        {
            //        //            numPatternB.Value = numPatternG.Value = numPatternR.Value = Parttern_RM67350[CAS200_Measure_Count];
            //        //        }
            //        //        Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
            //        //    }
            //        //    else
            //        //    {
            //        //        CAS200_Measure_Count = 0;
            //        //        T3.Enabled = false;
            //        //    }
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 1) //rm67120
            //        //{
            //        //    if (CAS200_Measure_Count < Parttern_RM67120.Length)
            //        //    {
            //        //        if (numPatternR.Value * numPatternG.Value * numPatternB.Value == 0)
            //        //        {
            //        //            if (numPatternR.Value != 0)
            //        //            {
            //        //                numPatternR.Value = Parttern_RM67120[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternG.Value != 0)
            //        //            {
            //        //                numPatternG.Value = Parttern_RM67120[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternB.Value != 0)
            //        //            {
            //        //                numPatternB.Value = Parttern_RM67120[CAS200_Measure_Count];
            //        //            }
            //        //        }
            //        //        else
            //        //        {
            //        //            numPatternB.Value = numPatternG.Value = numPatternR.Value = Parttern_RM67120[CAS200_Measure_Count];
            //        //        }
            //        //        Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
            //        //    }
            //        //    else
            //        //    {
            //        //        CAS200_Measure_Count = 0;
            //        //        T3.Enabled = false;
            //        //    }
            //        //}
            //        //else if ((MainForm.IC_comboBox.SelectedIndex == 2) || (MainForm.IC_comboBox.SelectedIndex == 3)) //rm67160/62
            //        //{
            //        //    if (CAS200_Measure_Count < Parttern_RM67162.Length)
            //        //    {
            //        //        if (numPatternR.Value * numPatternG.Value * numPatternB.Value == 0)
            //        //        {
            //        //            if (numPatternR.Value != 0)
            //        //            {
            //        //                numPatternR.Value = Parttern_RM67162[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternG.Value != 0)
            //        //            {
            //        //                numPatternG.Value = Parttern_RM67162[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternB.Value != 0)
            //        //            {
            //        //                numPatternB.Value = Parttern_RM67162[CAS200_Measure_Count];
            //        //            }
            //        //        }
            //        //        else
            //        //        {
            //        //            numPatternB.Value = numPatternG.Value = numPatternR.Value = Parttern_RM67162[CAS200_Measure_Count];
            //        //        }
            //        //        Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
            //        //    }
            //        //    else
            //        //    {
            //        //        CAS200_Measure_Count = 0;
            //        //        T3.Enabled = false;
            //        //    }
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 7) //HX83200
            //        //{
            //        //    if (CAS200_Measure_Count < Parttern_RM67162.Length)
            //        //    {
            //        //        if (numPatternR.Value * numPatternG.Value * numPatternB.Value == 0)
            //        //        {
            //        //            if (numPatternR.Value != 0)
            //        //            {
            //        //                numPatternR.Value = Parttern_RM67162[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternG.Value != 0)
            //        //            {
            //        //                numPatternG.Value = Parttern_RM67162[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternB.Value != 0)
            //        //            {
            //        //                numPatternB.Value = Parttern_RM67162[CAS200_Measure_Count];
            //        //            }
            //        //        }
            //        //        else
            //        //        {
            //        //            numPatternB.Value = numPatternG.Value = numPatternR.Value = Parttern_RM67162[CAS200_Measure_Count];
            //        //        }
            //        //        Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
            //        //    }
            //        //    else
            //        //    {
            //        //        CAS200_Measure_Count = 0;
            //        //        T3.Enabled = false;
            //        //    }
            //        //}
            //        //else if (MainForm.IC_comboBox.SelectedIndex == 13) //R66455
            //        //{
            //        //    if (CAS200_Measure_Count < Parttern_R66455.Length)
            //        //    {
            //        //        if (numPatternR.Value * numPatternG.Value * numPatternB.Value == 0)
            //        //        {
            //        //            if (numPatternR.Value != 0)
            //        //            {
            //        //                numPatternR.Value = Parttern_R66455[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternG.Value != 0)
            //        //            {
            //        //                numPatternG.Value = Parttern_R66455[CAS200_Measure_Count];
            //        //            }
            //        //            if (numPatternB.Value != 0)
            //        //            {
            //        //                numPatternB.Value = Parttern_R66455[CAS200_Measure_Count];
            //        //            }
            //        //        }
            //        //        else
            //        //        {
            //        //            numPatternB.Value = numPatternG.Value = numPatternR.Value = Parttern_R66455[CAS200_Measure_Count];
            //        //        }
            //        //        Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
            //        //    }
            //        //    else
            //        //    {
            //        //        CAS200_Measure_Count = 0;
            //        //        T3.Enabled = false;
            //        //    }
            //        //}
            //    }
            //    else
            //    {
            //        if ((Convert.ToInt16(numPatternR.Value.ToString()) + Convert.ToInt16(numPatternB.Value.ToString()) + Convert.ToInt16(numPatternG.Value.ToString()) - int.Parse(CAS200_txt0.Text)) >= 0)
            //        {
            //            if (numPatternR.Value * numPatternG.Value * numPatternB.Value == 0)
            //            {
            //                if (numPatternR.Value != 0)
            //                {
            //                    numPatternR.Value = (byte)(Convert.ToInt16(numPatternR.Value.ToString()) - int.Parse(CAS200_txt0.Text));
            //                }
            //                if (numPatternG.Value != 0)
            //                {
            //                    numPatternG.Value = (byte)(Convert.ToInt16(numPatternG.Value.ToString()) - int.Parse(CAS200_txt0.Text));
            //                }
            //                if (numPatternB.Value != 0)
            //                {
            //                    numPatternB.Value = (byte)(Convert.ToInt16(numPatternB.Value.ToString()) - int.Parse(CAS200_txt0.Text));
            //                }
            //            }
            //            else
            //            {
            //                numPatternB.Value = numPatternG.Value = numPatternR.Value = (byte)(Convert.ToInt16(numPatternR.Value.ToString()) - int.Parse(CAS200_txt0.Text));
            //            }
            //            Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
            //        }
            //        else
            //        {
            //            T3.Enabled = false;
            //        }
            //    }
            //    Thread.Sleep(50);
            //    btn_CAS200_Synt_Click(btn_CAS200_Measure, null); //量测
        }

        private void btn_CAS200_Synt_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;

            switch (btnTmp.Name)
            {
                case "btn_CAS200_Measure":
                    {
                        Msr_CA310();
                        CAS200_Measure_Information(auto_lum, auto_x, auto_y);
                    }
                    break;
                case "btn_CAS200Save":
                    {
                        SaveFileDialog sf = new SaveFileDialog(); //对话框
                        sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型

                        string str = "序号,x坐标,y坐标,亮度\r\n";
                        if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                        {
                            if (sf.FileName == "") return; //如果没有输入文件名
                            try
                            {
                                StreamWriter sw = null; //文件流
                                sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                                for (int i = 0; i < (this.CAS200_dataGridView.Rows.Count - 1); i++)
                                {
                                    str = str + this.CAS200_dataGridView.Rows[i].Cells[0].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[1].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[2].Value.ToString() + ","
                                        + this.CAS200_dataGridView.Rows[i].Cells[3].Value.ToString() + "\r\n";

                                }
                                sw.WriteLine(str);
                                sw.Dispose(); //释放资源
                                sw.Close(); //关闭数据流
                                MessageBox.Show("CA310数据保存OK！", "系统提示：");
                            }
                            catch
                            {
                                MessageBox.Show("CA310数据保存时发生错误，请再试！", "系统提示：");
                            }
                        }
                    }
                    break;
                case "btn_CAS200_load":
                    {
                        OpenFileDialog sf = new OpenFileDialog();
                        int Line_Count = 0;
                        string strtmp = "";
                        sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义文件的类型
                        sf.Title = "数据导入";
                        if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
                        {
                            FileStream fs = new FileStream(sf.FileName, FileMode.Open, FileAccess.Read);
                            StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                            streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                            string strLine = streamReader.ReadLine();
                            while (!string.IsNullOrEmpty(strLine))
                            {
                                strtmp = strtmp + strLine + ",";
                                strLine = streamReader.ReadLine(); //循环读取下一行
                                Line_Count++;
                            }
                            fs.Close(); //关闭流
                            streamReader.Close();
                            string[] arrtmp = strtmp.Split(new Char[] { ',' });
                            for (int j = 1; j < Line_Count - 1; j++)
                            {
                                DataGridViewRow Row1 = new DataGridViewRow();
                                CAS200_dataGridView.Rows.Add(Row1);
                            }
                            try
                            {
                                for (int i = 0; i < Line_Count - 1; i++)//有一行标题
                                {
                                    CAS200_dataGridView.Rows[i].Cells[0].Value = arrtmp[4 * (i + 1) + 0];
                                    CAS200_dataGridView.Rows[i].Cells[1].Value = arrtmp[4 * (i + 1) + 1];
                                    CAS200_dataGridView.Rows[i].Cells[2].Value = arrtmp[4 * (i + 1) + 2];
                                    CAS200_dataGridView.Rows[i].Cells[3].Value = arrtmp[4 * (i + 1) + 3];
                                }
                                MessageBox.Show("CA310数据导入OK！", "提示：");
                            }
                            catch
                            {
                                MessageBox.Show("CA310数据导入时发生错误！", "提示：");
                            }
                        }
                    }
                    break;
                case "btn_CAS200_Clean":
                    {
                        DialogResult dr = MessageBox.Show("确认清空列表数据吗？", "系统提示", MessageBoxButtons.OKCancel);
                        if (dr == DialogResult.OK)
                        {
                            this.CAS200_dataGridView.Rows.Clear();
                        }
                        else {; }
                    }
                    break;
                case "btn_CAS200_Delect":
                    {
                        int Current_Index, Row_Accum;
                        Current_Index = this.CAS200_dataGridView.SelectedCells[0].RowIndex;
                        DataGridViewRow Row1 = CAS200_dataGridView.Rows[Current_Index];
                        this.CAS200_dataGridView.Rows.Remove(Row1);
                        this.CAS200_dataGridView.Rows[Current_Index].Selected = false;
                        Row_Accum = this.CAS200_dataGridView.Rows.Count - 1;
                        for (Current_Index = 0; Current_Index < Row_Accum; Current_Index++)
                        {
                            this.CAS200_dataGridView.Rows[Current_Index].Cells[0].Value = Current_Index + 1;//序号
                        }
                        this.CAS200_dataGridView.Rows[Row_Accum - 1].Selected = true;
                        CAS200_dataGridView.FirstDisplayedScrollingRowIndex = CAS200_dataGridView.RowCount - 1;
                    }
                    break;

                case "btn_CAS200_Stop":
                    {
                        T3.Enabled = false;
                    }
                    break;
                case "btn_CAS200_Start":
                    {
                        string strLine = Convert.ToString(text_CE_data.Text);
                        string[] arr = strLine.Split(','); //以逗号分割，添加到数组
                                                           //  Pattern_Write((byte)(numPatternR.Value), (byte)(numPatternG.Value), (byte)(numPatternB.Value));
                        CAS200_Measure_Count = 0;
                        CAS200_Measure_Gray = 255;
                        CA200_Measure_CE = byte.Parse((Convert.ToInt32(arr[arr.Length - 1], 16).ToString()));
                        T3.Enabled = true;
                        T3.Interval = int.Parse(CAS200_txt1.Text);//延时
                    }
                    break;
                default: break;
            }
        }

        private void btnCal_Click(object sender, EventArgs e)
        {
            if (btnCal_manual.Text == "0Cal")
                btnCal_manual.Text = "Wait";
            ca310info.Visible = true;
            try
            {
                Ca310Obj.CA310_Connect();
                Ca310Obj.CA310_Initial(byte.Parse(MainForm.CA310_Channel.Text));

                btnAutogammas350.Enabled = true;
                CAS200_dataGridView.Enabled = true;
                btn_CAS200Save.Enabled = true;
                btn_CAS200_Measure.Enabled = true;
                btn_CAS200_Clean.Enabled = true;
                btn_CAS200_Delect.Enabled = true;
                gBx_CAS200_Auto_Test.Enabled = true;
            }
            catch (Exception EX)
            {
                MessageBox.Show("CA310 Connect Fail!!" + EX.Message);
            }


            btnCal_manual.Text = "0Cal";
            ca310info.Visible = false;
            ca310info.Enabled = true;
        }

        private void Msr_CA310()
        {
            try
            {
                MainForm.Msr_CASDK();
                auto_lum = MainForm.CASDK_LUM;
                auto_x = MainForm.CASDK_x;
                auto_y = MainForm.CASDK_y;
            }
            catch
            {
                MessageBox.Show("请校准Senser！");
            }
        }

        //=================================LCD cfg function: OLED_Show_Click====end=================


        private int adjust_num = 0;
        private string adjust_class = null;
        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private double D_x, D_y;
        //private double lum_off, x_off, y_off;
        private int index = 0;
        //private int flag1 = 1;
        //private int flag2 = 1;
        //private int flag3 = 1;
        private int adjust_Rnum = 0;
        private int adjust_Gnum = 0;
        private int adjust_Bnum = 0;
        private int adjust_l = 0;
        private int adjust_x = 0;
        private int adjust_y = 0;
        //private int step_l = 1;
        //private int step_m = 5;
        //private int step_h = 12;
        Gamma_Addr GAMMAs = new Gamma_Addr();
        private ushort[] gamma_r = new ushort[31];
        private ushort[] gamma_g = new ushort[31];
        private ushort[] gamma_b = new ushort[31];
        private double lum_off, x_off, y_off;
        private double R_x = 0.67;//
        private double R_y = 0.33;
        private double G_x = 0.22;
        private double G_y = 0.71;

        private void btnBack_Click(object sender, EventArgs e)
        {
            MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(SerialPort1_DataReceived);//移除本窗口串口处理函数
            MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(MainForm.SerialPort1_DataReceived);//
            MainForm.Show();
            this.Hide();
        }

        private void rm69350_KeyDown(object sender, KeyEventArgs e)
        {

            //Thread.Sleep(100);
            //if ((e.KeyCode == Keys.N) | (e.KeyCode == Keys.Left))// ;//&& Keys.A == e.KeyCode)
            //{
            //    Demura_Function_ON.PerformClick();
            //}
            //else if ((e.KeyCode == Keys.M) | (e.KeyCode == Keys.Right))// ;//&& Keys.A == e.KeyCode)
            //{
            //    Demura_Function_OFF.PerformClick();
            //}
        }




        private void btnModeSet_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;
            //Send_buf[0] = 0x0E;///RM67160/67162
            Send_buf[0] = 0x52;///RM69310
            Send_buf[2] = 0x02;



            switch (btnTmp.Name)
            {
                case "btnRegW":         //w写寄存器
                    {
                        Send_buf[1] = 0x01;
                        Send_buf[3] = byte.Parse((Convert.ToInt32(txtRM67160Add.Text.Trim(), 16)).ToString());
                        Send_buf[4] = byte.Parse((Convert.ToInt32(txtRM67160Data.Text.Trim(), 16)).ToString());
                    }
                    break;

                case "btnRegR":
                    {
                        Setting.TempRecord.RM67160CmdType = 0x01;   //读普通寄存器
                                                                    /////
                        if (radioButton_CMD1.Checked == true)
                        {
                            Send_buf[4] = 0x00;
                        }
                        else
                        {
                            if (radioButton_P40.Checked == true)
                            {
                                Send_buf[4] = 0x01;
                            }
                            else if (radioButton_P50.Checked == true)
                            {
                                Send_buf[4] = 0x02;
                            }
                            else if (radioButton_P60.Checked == true)
                            {
                                Send_buf[4] = 0x03;
                            }
                            else if (radioButton_P70.Checked == true)
                            {
                                Send_buf[4] = 0x04;
                            }
                        }
                        Send_buf[1] = 0x02;
                        Send_buf[3] = byte.Parse((Convert.ToInt32(txtRM67160Add.Text.Trim(), 16)).ToString());
                    }
                    break;
                //-------------------------------------------    

                case "otp":         //w写gamma寄存器
                    {
                        Send_buf[1] = 0x05;
                        Send_buf[4] = 0x00;
                        if (checkBox_40.Checked == true)
                        {
                            Send_buf[4] += 0x01;
                        }
                        if (checkBox_50.Checked == true)
                        {
                            Send_buf[4] += 0x02;
                        }
                        if (checkBox_60.Checked == true)
                        {
                            Send_buf[4] += 0x04;
                        }
                        if (checkBox_70.Checked == true)
                        {
                            Send_buf[4] += 0x08;
                        }
                    }
                    break;
                case "otp_read":         //读gamma寄存器
                    {
                        Send_buf[1] = 0x06;

                    }
                    break;

                default:
                    break;
            }

            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                MainForm.comPort.Write(Send_buf, 0, 7);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 3000;
            T1.Enabled = true;

        }

        private void RM69310_gamma_clac_tx()
        {
            //int gamma_gray_level_num = 154;
            //int i = 0;
            int max_grey_level = 0x3FF;

            //for (i = 0; i < gamma_gray_level_num; i++)
            {
                //GAMMA_69310_Point25_Bufdata[i] = Convert.ToByte(Convert.ToUInt16(NoRMB196.Value.ToString()) >> 8);
                //GAMMA_69310_Point25_Bufdata[i] = Convert.ToByte((Convert.ToUInt16(NoRMB196.Value.ToString())) & 0xff);
            }

            ////R  gamma
            GAMMA_69310_Point25_BufdataR[0] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR0.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[1] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR1.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[2] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR2.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[3] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR3.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[4] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR4.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[5] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR5.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[6] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR6.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[7] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR7.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[8] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR8.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[9] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR9.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[10] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR10.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[11] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR11.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[12] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR12.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[13] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR13.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[14] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR14.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[15] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR15.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[16] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR16.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[17] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR17.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[18] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR18.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[19] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR19.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[20] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR20.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[21] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR21.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[22] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR22.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[23] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR23.Value.ToString()));
            GAMMA_69310_Point25_BufdataR[24] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR24.Value.ToString()));

            ////G  gamma
            GAMMA_69310_Point25_BufdataG[0] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG0.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[1] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG1.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[2] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG2.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[3] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG3.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[4] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG4.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[5] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG5.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[6] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG6.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[7] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG7.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[8] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG8.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[9] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG9.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[10] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG10.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[11] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG11.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[12] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG12.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[13] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG13.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[14] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG14.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[15] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG15.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[16] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG16.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[17] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG17.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[18] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG18.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[19] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG19.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[20] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG20.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[21] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG21.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[22] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG22.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[23] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG23.Value.ToString()));
            GAMMA_69310_Point25_BufdataG[24] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG24.Value.ToString()));

            ////B  gamma
            GAMMA_69310_Point25_BufdataB[0] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB0.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[1] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB1.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[2] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB2.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[3] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB3.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[4] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB4.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[5] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB5.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[6] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB6.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[7] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB7.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[8] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB8.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[9] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB9.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[10] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB10.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[11] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB11.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[12] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB12.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[13] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB13.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[14] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB14.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[15] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB15.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[16] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB16.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[17] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB17.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[18] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB18.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[19] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB19.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[20] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB20.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[21] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB21.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[22] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB22.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[23] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB23.Value.ToString()));
            GAMMA_69310_Point25_BufdataB[24] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB24.Value.ToString()));

            ////write R gamma
            for (int i = 0x00; i <= 0x33; i++)
            {
                if ((i != 0x2c) || (i != 0x2e))
                {

                    Send_buf[4 + 2 * i + 0] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[i]) >> 8);
                    Send_buf[4 + 2 * i + 1] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[i]) & 0xFF);
                }

            }
            ////write G gamma
            for (int i = (0x34 - 0x34); i <= (0x68 - 0x34); i++)
            {
                if ((i != 0x3c) || (i != 0x3e))
                {

                    Send_buf[4 + 2 * (i + 0x34) + 0] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[i]) >> 8);
                    Send_buf[4 + 2 * (i + 0x34) + 1] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[i]) & 0xFF);
                }

            }
            ////write B gamma
            for (int i = (0x69 - 0x69); i <= (0x9b - 0x69); i++)
            {
                if ((i != 0x57))
                {

                    Send_buf[4 + 2 * (i + 0x69) + 0] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[i]) >> 8);
                    Send_buf[4 + 2 * (i + 0x69) + 1] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[i]) & 0xFF);
                }

            }

        }

        private void RM69310_gamma_clac_rx()
        {
            int max_grey_level = 0x3F;



        }


        private void RM69310_Gamma_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[162];
            int max_grey_level = 0x3FF;

            Setting.TempRecord.cmdLen = 162;
            Send_buf[2] = 162 - 5;
            Send_buf[3] = 0;
            Send_buf[0] = 0x52; //RM69310

            switch (btnTmp.Name)
            {
                case "RM69310_GammaW_all":
                    {
                        //Setting.TempRecord.RM69310CmdType = 0x03;       //      3
                        Send_buf[1] = 0x03;                             //写Gamma
                                                                        ////R  gamma
                        GAMMA_69310_Point25_BufdataR[0] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR0.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[1] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR1.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[2] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR2.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[3] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR3.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[4] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR4.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[5] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR5.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[6] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR6.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[7] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR7.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[8] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR8.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[9] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR9.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[10] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR10.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[11] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR11.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[12] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR12.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[13] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR13.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[14] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR14.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[15] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR15.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[16] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR16.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[17] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR17.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[18] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR18.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[19] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR19.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[20] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR20.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[21] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR21.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[22] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR22.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[23] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR23.Value.ToString()));
                        GAMMA_69310_Point25_BufdataR[24] = max_grey_level & (Convert.ToUInt16(rm69310_gammaR24.Value.ToString()));

                        ////G  gamma
                        GAMMA_69310_Point25_BufdataG[0] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG0.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[1] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG1.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[2] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG2.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[3] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG3.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[4] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG4.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[5] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG5.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[6] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG6.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[7] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG7.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[8] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG8.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[9] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG9.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[10] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG10.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[11] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG11.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[12] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG12.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[13] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG13.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[14] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG14.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[15] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG15.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[16] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG16.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[17] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG17.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[18] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG18.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[19] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG19.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[20] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG20.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[21] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG21.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[22] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG22.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[23] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG23.Value.ToString()));
                        GAMMA_69310_Point25_BufdataG[24] = max_grey_level & (Convert.ToUInt16(rm69310_gammaG24.Value.ToString()));

                        ////B  gamma
                        GAMMA_69310_Point25_BufdataB[0] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB0.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[1] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB1.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[2] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB2.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[3] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB3.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[4] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB4.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[5] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB5.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[6] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB6.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[7] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB7.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[8] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB8.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[9] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB9.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[10] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB10.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[11] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB11.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[12] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB12.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[13] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB13.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[14] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB14.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[15] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB15.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[16] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB16.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[17] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB17.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[18] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB18.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[19] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB19.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[20] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB20.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[21] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB21.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[22] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB22.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[23] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB23.Value.ToString()));
                        GAMMA_69310_Point25_BufdataB[24] = max_grey_level & (Convert.ToUInt16(rm69310_gammaB24.Value.ToString()));

                        ////R
                        Send_buf[4 + 0x00] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[0]) >> 8);
                        Send_buf[4 + 0x01] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[0]) & 0xFF);
                        Send_buf[4 + 0x02] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[1]) >> 8);
                        Send_buf[4 + 0x03] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[1]) & 0xFF);
                        Send_buf[4 + 0x04] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[2]) >> 8);
                        Send_buf[4 + 0x05] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[2]) & 0xFF);
                        Send_buf[4 + 0x06] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[3]) >> 8);
                        Send_buf[4 + 0x07] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[3]) & 0xFF);
                        Send_buf[4 + 0x08] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[4]) >> 8);
                        Send_buf[4 + 0x09] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[4]) & 0xFF);
                        Send_buf[4 + 0x0A] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[5]) >> 8);
                        Send_buf[4 + 0x0B] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[5]) & 0xFF);
                        Send_buf[4 + 0x0C] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[6]) >> 8);
                        Send_buf[4 + 0x0D] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[6]) & 0xFF);
                        Send_buf[4 + 0x0E] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[7]) >> 8);
                        Send_buf[4 + 0x0F] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[7]) & 0xFF);
                        Send_buf[4 + 0x10] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[8]) >> 8);
                        Send_buf[4 + 0x11] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[8]) & 0xFF);
                        Send_buf[4 + 0x12] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[9]) >> 8);
                        Send_buf[4 + 0x13] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[9]) & 0xFF);
                        Send_buf[4 + 0x14] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[10]) >> 8);
                        Send_buf[4 + 0x15] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[10]) & 0xFF);
                        Send_buf[4 + 0x16] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[11]) >> 8);
                        Send_buf[4 + 0x17] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[11]) & 0xFF);
                        Send_buf[4 + 0x18] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[12]) >> 8);
                        Send_buf[4 + 0x19] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[12]) & 0xFF);
                        Send_buf[4 + 0x1A] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[13]) >> 8);
                        Send_buf[4 + 0x1B] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[13]) & 0xFF);
                        Send_buf[4 + 0x1C] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[14]) >> 8);
                        Send_buf[4 + 0x1D] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[14]) & 0xFF);
                        Send_buf[4 + 0x1E] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[15]) >> 8);
                        Send_buf[4 + 0x1F] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[15]) & 0xFF);
                        Send_buf[4 + 0x20] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[16]) >> 8);
                        Send_buf[4 + 0x21] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[16]) & 0xFF);
                        Send_buf[4 + 0x22] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[17]) >> 8);
                        Send_buf[4 + 0x23] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[17]) & 0xFF);
                        Send_buf[4 + 0x24] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[18]) >> 8);
                        Send_buf[4 + 0x25] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[18]) & 0xFF);
                        Send_buf[4 + 0x26] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[19]) >> 8);
                        Send_buf[4 + 0x27] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[19]) & 0xFF);
                        Send_buf[4 + 0x28] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[20]) >> 8);
                        Send_buf[4 + 0x29] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[20]) & 0xFF);
                        Send_buf[4 + 0x2A] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[21]) >> 8);
                        Send_buf[4 + 0x2B] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[21]) & 0xFF);
                        Send_buf[4 + 0x2D] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[22]) >> 8);
                        Send_buf[4 + 0x2F] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[22]) & 0xFF);
                        Send_buf[4 + 0x30] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[23]) >> 8);
                        Send_buf[4 + 0x31] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[23]) & 0xFF);
                        Send_buf[4 + 0x32] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[24]) >> 8);
                        Send_buf[4 + 0x33] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[24]) & 0xFF);

                        ///G
                        Send_buf[4 + 0x34 + 0x00] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[0]) >> 8);
                        Send_buf[4 + 0x34 + 0x01] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[0]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x02] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[1]) >> 8);
                        Send_buf[4 + 0x34 + 0x03] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[1]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x04] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[2]) >> 8);
                        Send_buf[4 + 0x34 + 0x05] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[2]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x06] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[3]) >> 8);
                        Send_buf[4 + 0x34 + 0x07] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[3]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x09] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[4]) >> 8);
                        Send_buf[4 + 0x34 + 0x0B] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[4]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x0C] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[5]) >> 8);
                        Send_buf[4 + 0x34 + 0x0D] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[5]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x0E] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[6]) >> 8);
                        Send_buf[4 + 0x34 + 0x0F] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[6]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x10] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[7]) >> 8);
                        Send_buf[4 + 0x34 + 0x11] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[7]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x12] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[8]) >> 8);
                        Send_buf[4 + 0x34 + 0x13] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[8]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x14] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[9]) >> 8);
                        Send_buf[4 + 0x34 + 0x15] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[9]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x16] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[10]) >> 8);
                        Send_buf[4 + 0x34 + 0x17] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[10]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x18] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[11]) >> 8);
                        Send_buf[4 + 0x34 + 0x19] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[11]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x1A] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[12]) >> 8);
                        Send_buf[4 + 0x34 + 0x1B] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[12]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x1C] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[13]) >> 8);
                        Send_buf[4 + 0x34 + 0x1D] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[13]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x1E] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[14]) >> 8);
                        Send_buf[4 + 0x34 + 0x1F] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[14]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x20] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[15]) >> 8);
                        Send_buf[4 + 0x34 + 0x21] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[15]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x22] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[16]) >> 8);
                        Send_buf[4 + 0x34 + 0x24] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[16]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x25] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[17]) >> 8);
                        Send_buf[4 + 0x34 + 0x26] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[17]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x27] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[18]) >> 8);
                        Send_buf[4 + 0x34 + 0x28] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[18]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x29] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[19]) >> 8);
                        Send_buf[4 + 0x34 + 0x2A] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[19]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x2B] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[20]) >> 8);
                        Send_buf[4 + 0x34 + 0x2C] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[20]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x2D] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[21]) >> 8);
                        Send_buf[4 + 0x34 + 0x2E] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[21]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x2F] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[22]) >> 8);
                        Send_buf[4 + 0x34 + 0x30] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[22]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x31] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[23]) >> 8);
                        Send_buf[4 + 0x34 + 0x32] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[23]) & 0xFF);
                        Send_buf[4 + 0x34 + 0x33] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[24]) >> 8);
                        Send_buf[4 + 0x34 + 0x34] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[24]) & 0xFF);

                        ////R
                        Send_buf[4 + 0x69 + 0x00] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[0]) >> 8);
                        Send_buf[4 + 0x69 + 0x01] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[0]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x02] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[1]) >> 8);
                        Send_buf[4 + 0x69 + 0x03] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[1]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x04] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[2]) >> 8);
                        Send_buf[4 + 0x69 + 0x05] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[2]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x06] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[3]) >> 8);
                        Send_buf[4 + 0x69 + 0x07] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[3]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x08] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[4]) >> 8);
                        Send_buf[4 + 0x69 + 0x09] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[4]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x0A] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[5]) >> 8);
                        Send_buf[4 + 0x69 + 0x0B] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[5]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x0C] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[6]) >> 8);
                        Send_buf[4 + 0x69 + 0x0D] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[6]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x0E] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[7]) >> 8);
                        Send_buf[4 + 0x69 + 0x0F] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[7]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x10] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[8]) >> 8);
                        Send_buf[4 + 0x69 + 0x11] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[8]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x12] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[9]) >> 8);
                        Send_buf[4 + 0x69 + 0x13] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[9]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x14] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[10]) >> 8);
                        Send_buf[4 + 0x69 + 0x15] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[10]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x16] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[11]) >> 8);
                        Send_buf[4 + 0x69 + 0x17] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[11]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x18] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[12]) >> 8);
                        Send_buf[4 + 0x69 + 0x19] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[12]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x1A] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[13]) >> 8);
                        Send_buf[4 + 0x69 + 0x1B] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[13]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x1C] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[14]) >> 8);
                        Send_buf[4 + 0x69 + 0x1D] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[14]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x1E] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[15]) >> 8);
                        Send_buf[4 + 0x69 + 0x1F] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[15]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x20] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[16]) >> 8);
                        Send_buf[4 + 0x69 + 0x21] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[16]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x22] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[17]) >> 8);
                        Send_buf[4 + 0x69 + 0x23] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[17]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x24] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[18]) >> 8);
                        Send_buf[4 + 0x69 + 0x25] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[18]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x26] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[19]) >> 8);
                        Send_buf[4 + 0x69 + 0x27] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[19]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x28] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[20]) >> 8);
                        Send_buf[4 + 0x69 + 0x29] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[20]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x2A] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[21]) >> 8);
                        Send_buf[4 + 0x69 + 0x2B] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[21]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x2C] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[22]) >> 8);
                        Send_buf[4 + 0x69 + 0x2D] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[22]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x2E] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[23]) >> 8);
                        Send_buf[4 + 0x69 + 0x2F] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[23]) & 0xFF);
                        Send_buf[4 + 0x69 + 0x30] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[24]) >> 8);
                        Send_buf[4 + 0x69 + 0x31] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[24]) & 0xFF);

                        ////
                        if (RM69310_11GP.Checked == true)
                        {
                            Send_buf[4 + 0x9B] = 0x10;
                        }
                        else
                        {
                            Send_buf[4 + 0x9B] = 0x00;
                        }
                        //////////NG
                        //int reg_y = 0;
                        //////write R gamma
                        //for (int i = 0x00; i <= 24; i++)
                        //{

                        //    {
                        //        if (((4 + 2 * i + 0 + reg_y) == 0x2c) )
                        //        {
                        //            reg_y++;
                        //        }
                        //        Send_buf[4 + 2 * i + 0 + reg_y] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[i]) >> 8);
                        //        if ( ((4 + 2 * i + 0 + reg_y) == 0x2e))
                        //        {
                        //            reg_y++;
                        //        }
                        //        Send_buf[4 + 2 * i + 1 + reg_y] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataR[i]) & 0xFF);

                        //    }

                        //}
                        //////write G gamma
                        //for (int i = 0x00; i <= 24; i++)
                        //{
                        //    //if ((i != 0x3c) || (i != 0x3e))
                        //    {
                        //        if (((4 + 0x34 + 2 * (i) + 0 + reg_y) == 0x3c) )
                        //        {
                        //            reg_y++;
                        //        }
                        //        Send_buf[4 + 0x34  + 2 * (i ) + 0 + reg_y] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[i]) >> 8);
                        //        if (((4 + 0x34 + 2 * (i) + 1 + reg_y) == 0x3e)|| ((4 + 0x34 + 2 * (i) + 1 + reg_y) == 0x57))
                        //        {
                        //            reg_y++;
                        //        }
                        //        Send_buf[4 + 0x34 + 2 * (i ) + 1 + reg_y] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataG[i]) & 0xFF);
                        //    }

                        //}
                        //////write B gamma
                        //for (int i = 0x00; i <= 24; i++)
                        //{
                        //    //if ((i != 0x57))
                        //    {

                        //        Send_buf[4 + 0x69 + 2 * (i ) + 0 + reg_y] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[i]) >> 8);
                        //        Send_buf[4 + 0x69 + 2 * (i ) + 1 + reg_y] = Convert.ToByte(Convert.ToUInt16(GAMMA_69310_Point25_BufdataB[i]) & 0xFF);
                        //    }

                        //}
                    }
                    break;

                case "RM69310_GammaR_all":
                    {
                        //Setting.TempRecord.RM69310CmdType = 0x03;       //      3
                        Send_buf[1] = 0x04;                             //读Gamma

                    }
                    break;

                default:
                    break;

            }


            CRCData.CalculateCrc16(Send_buf, out Send_buf[162 - 2], out Send_buf[162 - 1]);
            try
            {
                RM350_0XFE_XX = 0x00;
                MainForm.comPort.Write(Send_buf, 0, 162);   //RM69310
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            T1.Enabled = false;
            T1.Interval = 2000;
            T1.Enabled = true;
        }



        private void trackBar2_Scroll(object sender, EventArgs e)
        {
            R69350_51Reg.Text = trackBar_RM69350.Value.ToString() + "阶";
            if (RM350_0XFE_XX != 0xF0)
            {
                RM350_0XFE_XX = 0xF0;
                txtRM67160Add.Text = "fe";
                txtRM67160Data.Text = "00";
                btnModeSet_Click(btnRegW, e);
            }
            txtRM67160Add.Text = "51";
            txtRM67160Data.Text = trackBar_RM69350.Value.ToString("x2");
            btnModeSet_Click(btnRegW, e);
        }

        private static bool Gamma_adjust_timeout_flag = false;
        private static int Gamma_auto_adjust_count = 0;
        //private int flag1 = 1;
        //private int flag2 = 1;
        //private int flag3 = 1;
        private void R6645x_Gamma_auto_Adujst(int lum_adjust_enable, int x_adjust_enable, int y_adjust_enable, int steph, int stepm, int stepl,
        double measure_lum, double measure_x, double measure_y, double target_lum, double target_x, double target_y, int adjust_times,
        decimal input_reg_red, decimal input_reg_green, decimal input_reg_blue,
        //低阶绑点调用
        int gary_last_index2, int gary_last_index1, int gary_target,
        decimal index2_reg_red, decimal index2_reg_green, decimal index2_reg_blue,
        decimal index1_reg_red, decimal index1_reg_green, decimal index1_reg_blue,

        out decimal output_reg_red, out decimal output_reg_green, out decimal output_reg_blue, out bool timeout_flag)
        {
            int step_tmp = 0;

            timeout_flag = false;
            output_reg_red = input_reg_red;
            output_reg_green = input_reg_green;
            output_reg_blue = input_reg_blue;

            try
            {
                if (Gamma_auto_adjust_count <= adjust_times)
                {
                    Gamma_auto_adjust_count = Gamma_auto_adjust_count + 1;
                    if (x_adjust_enable == 1)
                    {
                        {
                            if (Math.Abs(measure_x - target_x) > target_x * 20 / 100)
                            {
                                if (target_lum < 1)
                                { step_tmp = stepm; }
                                else
                                { step_tmp = steph; }
                            }
                            else if (Math.Abs(measure_x - target_x) > target_x * 5 / 100)
                            {
                                if (target_lum < 1) //if lower target lum
                                { step_tmp = stepl; }
                                else
                                { step_tmp = stepm; }
                            }
                            else
                            {
                                step_tmp = stepl;
                            }
                            if (measure_x > target_x)
                                output_reg_red = input_reg_red - step_tmp;
                            else output_reg_red = input_reg_red + step_tmp;
                        }
                    }
                    //-----------------------------------------------
                    if (lum_adjust_enable == 1)
                    {
                        if (Math.Abs(measure_lum - target_lum) > target_lum * 20 / 100)
                        {
                            if (target_lum < 1)
                            { step_tmp = stepm; }
                            else
                            { step_tmp = steph; }
                        }
                        else if (Math.Abs(measure_lum - target_lum) > target_lum * 2 / 100)
                        {
                            if (target_lum < 1)
                            { step_tmp = stepl; }
                            else
                            { step_tmp = stepm; }
                        }
                        else
                        {
                            step_tmp = stepl;
                        }
                        if (measure_lum > target_lum)
                            output_reg_green = input_reg_green - step_tmp;
                        else output_reg_green = input_reg_green + step_tmp;
                    }
                    //-----------------------------------------
                    if (y_adjust_enable == 1)
                    {
                        {
                            if (Math.Abs(measure_y - target_y) > target_y * 20 / 100)
                            {
                                if (target_lum < 1)
                                { step_tmp = stepm; }
                                else
                                { step_tmp = steph; }
                            }
                            else if (Math.Abs(measure_y - target_y) > target_y * 5 / 100)
                            {
                                if (target_lum < 1)
                                { step_tmp = stepl; }
                                else
                                { step_tmp = stepm; }
                            }
                            else
                            {
                                step_tmp = stepl;
                            }
                            if (measure_y > target_y)
                                output_reg_blue = input_reg_blue - step_tmp;
                            else output_reg_blue = input_reg_blue + step_tmp;
                        }
                    }
                    //----------------------------------若数值超限--------------------------------------

                    if (((output_reg_red < 30) || (output_reg_red > index2_reg_red) || (output_reg_green < 30) || (output_reg_green > index2_reg_green) || (output_reg_blue < 30) || (output_reg_blue > index2_reg_blue)) && ((target_lum * 95 / 100 < measure_lum) && (target_lum * 105 / 100 > measure_lum)))
                    {
                        output_reg_red = Convert.ToInt16(index1_reg_red - Math.Abs(index2_reg_red - index1_reg_red) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target));
                        output_reg_green = Convert.ToInt16(index1_reg_green - Math.Abs(index2_reg_green - index1_reg_green) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target));
                        output_reg_blue = Convert.ToInt16(index1_reg_blue - Math.Abs(index2_reg_blue - index1_reg_blue) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target));
                        textBox1.Text = "\n" + gary_target + "---该绑点R/G/B插值迭代\r";
                        Gamma_auto_adjust_count = 0;
                        timeout_flag = true;
                    }
                }
                else
                {
                    textBox1.Text += "\n" + gary_target + " ---Timeout!\r\n";
                    Gamma_auto_adjust_count = 0;
                    timeout_flag = true;

                    if (output_reg_red > index1_reg_red)
                    {
                        output_reg_red = index1_reg_red - Math.Abs(index2_reg_red - index1_reg_red) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target);
                        textBox1.Text += "\n" + gary_target + "该绑点R差值迭代\r\n";
                    }
                    if (output_reg_green > index1_reg_green)
                    {
                        output_reg_green = index1_reg_green - Math.Abs(index2_reg_green - index1_reg_green) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target);
                        textBox1.Text += "\n" + gary_target + "该绑点G差值迭代\r\n";
                    }
                    if (output_reg_blue > index1_reg_blue)
                    {
                        output_reg_blue = index1_reg_blue - Math.Abs(index2_reg_blue - index1_reg_blue) / (gary_last_index2 - gary_last_index1) * (gary_last_index1 - gary_target);
                        textBox1.Text += "\n" + gary_target + "该绑点B差值迭代\r\n";
                    }
                }
                output_reg_red = Convert.ToInt32(output_reg_red);
                output_reg_green = Convert.ToInt32(output_reg_green);
                output_reg_blue = Convert.ToInt32(output_reg_blue);
            }
            catch
            {
                ;
            }

        }

        void auto_gamma_write(UInt16 addr, UInt16 datar, UInt16 addg, UInt16 datag, UInt16 addb, UInt16 Datab)//一组gamma写入
        {
            Send_buf = new byte[17];
            Setting.TempRecord.cmdLen = 17;

            if ((datar < 0x0001) || (datag < 0x0001) || (Datab < 0x0001) || (datar > 0x03FF) || (datag > 0x03FF) || (Datab > 0x03FF))
            {
                T5.Enabled = false;
                //T6.Enabled = false;
                //T7.Enabled = false;
                //T8.Enabled = false;
                //T12.Enabled = false;
                //T13.Enabled = false;
                //T14.Enabled = false;
                //T15.Enabled = false;
                flag1 = 1;
                flag2 = 1;
                flag3 = 1;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;
                MessageBox.Show("数值超出界限，已停止gamma调试", "提示");
            }

            //RM69310_Gamma_Click(RM69310_GammaW_all, null);
            //Thread.Sleep(15); //1秒=1000毫秒，


            switch (MainForm.IC_comboBox.SelectedIndex)
            {
                case 0:
                    Send_buf[0] = 0x09;//195
                    break;
                case 1:
                    Send_buf[0] = 0x08;//120
                    break;
                case 2:
                    Send_buf[0] = 0x0E;//160
                    break;
                case 3:
                    Send_buf[0] = 0x1E;//162
                    break;
                case 4:
                    Send_buf[0] = 0x19;//295
                    break;
                case 5:
                    Send_buf[0] = 0x29;//198
                    break;
                case 8:
                    Send_buf[0] = 0x2C;//300
                    break;
                case 10:
                    Send_buf[0] = 0x2e;//298
                    break;
                case 11:
                    Send_buf[0] = 0x3C;//350
                    break;
                case 12:
                    Send_buf[0] = 0x3D;//010
                    break;
                case 13:
                    Send_buf[0] = 0x30;//R66455
                    break;
                case 18:
                    Send_buf[0] = 0x52;//RM69310
                    break;
                default:
                    break;
            }
            Send_buf[1] = 0xDD;
            Send_buf[2] = 12;
            Send_buf[3] = Convert.ToByte(addr >> 8);
            Send_buf[4] = Convert.ToByte(datar >> 8);
            Send_buf[5] = Convert.ToByte(addr & 0xff);
            Send_buf[6] = Convert.ToByte(datar & 0xff);
            Send_buf[7] = Convert.ToByte(addg >> 8);
            Send_buf[8] = Convert.ToByte(datag >> 8);
            Send_buf[9] = Convert.ToByte(addg & 0xff);
            Send_buf[10] = Convert.ToByte(datag & 0xff);
            Send_buf[11] = Convert.ToByte(addb >> 8);
            Send_buf[12] = Convert.ToByte(Datab >> 8);
            Send_buf[13] = Convert.ToByte(addb & 0xff);
            Send_buf[14] = Convert.ToByte(Datab & 0xff);
            CRCData.CalculateCrc16(Send_buf, out Send_buf[15], out Send_buf[16]);

            try
            {
                MainForm.comPort.Write(Send_buf, 0, 17);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            System.Threading.Thread.Sleep(25); //1秒=1000毫秒，
        }


        private void RM350_OTP_Click(object sender, EventArgs e)
        {
            //Send_buf = new byte[7];
            //Button btnTmp = (Button)sender;
            //Setting.TempRecord.cmdLen = 7;

            //Send_buf[0] = 0x3C;        //RM69350
            //Send_buf[2] = 0x02;

            //switch (btnTmp.Name)
            //{
            //    case "RM350_OTP_GammaSet1": //otp GammaSet1       A
            //        {
            //            RM350_0XFE_XX = 0x00;
            //            Send_buf[1] = 0x0B;
            //            Setting.TempRecord.RM69350CmdType = 0x02;
            //            Send_buf[3] = 0x00;
            //            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            //            try
            //            {
            //                MainForm.comPort.Write(Send_buf, 0, 7);
            //            }
            //            catch
            //            {
            //                MessageBox.Show("串口有误，请检查", "错误");
            //            }
            //            T1.Enabled = false;
            //            T1.Interval = 2000;
            //            T1.Enabled = true;

            //            Delay(100);

            //            if (radioButton17.Checked == true)
            //            {
            //                RM350_0XFE_XX = 0x00;
            //                if (Convert.ToDecimal(textBox68.Text) > 3)
            //                {
            //                    MessageBox.Show("OTP次数不能大于3次", "警告");
            //                    return;
            //                }
            //                else
            //                {
            //                    RM350_0XFE_XX = 0x00;
            //                    Send_buf[1] = 0x0A;

            //                    CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            //                    try
            //                    {
            //                        MainForm.comPort.Write(Send_buf, 0, 7);
            //                    }
            //                    catch
            //                    {
            //                        MessageBox.Show("串口有误，请检查", "错误");
            //                    }
            //                    T1.Enabled = false;
            //                    T1.Interval = 2000;
            //                    T1.Enabled = true;
            //                }
            //            }

            //        }
            //        break;
            //    case "RM350_OTP_GammaSet2"://otp GammaSet2        D
            //        {
            //            RM350_0XFE_XX = 0x00;
            //            Send_buf[1] = 0x0B;
            //            Setting.TempRecord.RM69350CmdType = 0x02;
            //            Send_buf[3] = 0x01;
            //            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            //            try
            //            {
            //                MainForm.comPort.Write(Send_buf, 0, 7);
            //            }
            //            catch
            //            {
            //                MessageBox.Show("串口有误，请检查", "错误");
            //            }
            //            T1.Enabled = false;
            //            T1.Interval = 2000;
            //            T1.Enabled = true;

            //            Delay(100);

            //            if (radioButton16.Checked == true)
            //            {
            //                RM350_0XFE_XX = 0x00;
            //                if (Convert.ToDecimal(textBox68.Text) > 3)
            //                {
            //                    MessageBox.Show("OTP次数不能大于3次", "警告");
            //                    return;
            //                }
            //                else
            //                {
            //                    RM350_0XFE_XX = 0x00;
            //                    Send_buf[1] = 0x0D;

            //                    CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            //                    try
            //                    {
            //                        MainForm.comPort.Write(Send_buf, 0, 7);
            //                    }
            //                    catch
            //                    {
            //                        MessageBox.Show("串口有误，请检查", "错误");
            //                    }
            //                    T1.Enabled = false;
            //                    T1.Interval = 2000;
            //                    T1.Enabled = true;
            //                }
            //            }
            //        }
            //        break;
            //    case "RM350_OTP_GammaSet3"://otp GammaSet3        E
            //        {
            //            RM350_0XFE_XX = 0x00;
            //            Send_buf[1] = 0x0B;
            //            Setting.TempRecord.RM69350CmdType = 0x02;
            //            Send_buf[3] = 0x02;
            //            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            //            try
            //            {
            //                MainForm.comPort.Write(Send_buf, 0, 7);
            //            }
            //            catch
            //            {
            //                MessageBox.Show("串口有误，请检查", "错误");
            //            }
            //            T1.Enabled = false;
            //            T1.Interval = 2000;
            //            T1.Enabled = true;

            //            Delay(100);

            //            if (radioButton15.Checked == true)
            //            {
            //                RM350_0XFE_XX = 0x00;
            //                if (Convert.ToDecimal(textBox68.Text) > 3)
            //                {
            //                    MessageBox.Show("OTP次数不能大于3次", "警告");
            //                    return;
            //                }
            //                else
            //                {
            //                    RM350_0XFE_XX = 0x00;
            //                    Send_buf[1] = 0x0E;

            //                    CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            //                    try
            //                    {
            //                        MainForm.comPort.Write(Send_buf, 0, 7);
            //                    }
            //                    catch
            //                    {
            //                        MessageBox.Show("串口有误，请检查", "错误");
            //                    }
            //                    T1.Enabled = false;
            //                    T1.Interval = 2000;
            //                    T1.Enabled = true;
            //                }
            //            }
            //        }
            //        break;
            //    case "RM350_OTP_GOA"://otp goa                    F
            //        {
            //            RM350_0XFE_XX = 0x00;
            //            Send_buf[1] = 0x0B;
            //            Setting.TempRecord.RM69350CmdType = 0x02;
            //            Send_buf[3] = 0x03;
            //            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            //            try
            //            {
            //                MainForm.comPort.Write(Send_buf, 0, 7);
            //            }
            //            catch
            //            {
            //                MessageBox.Show("串口有误，请检查", "错误");
            //            }
            //            T1.Enabled = false;
            //            T1.Interval = 2000;
            //            T1.Enabled = true;

            //            Delay(100);

            //            if (radioButton14.Checked == true)
            //            {
            //                RM350_0XFE_XX = 0x00;
            //                if (Convert.ToDecimal(textBox68.Text) > 3)
            //                {
            //                    MessageBox.Show("OTP次数不能大于3次", "警告");
            //                    return;
            //                }
            //                else
            //                {
            //                    RM350_0XFE_XX = 0x00;
            //                    Send_buf[1] = 0x0F;

            //                    CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            //                    try
            //                    {
            //                        MainForm.comPort.Write(Send_buf, 0, 7);
            //                    }
            //                    catch
            //                    {
            //                        MessageBox.Show("串口有误，请检查", "错误");
            //                    }
            //                    T1.Enabled = false;
            //                    T1.Interval = 2000;
            //                    T1.Enabled = true;
            //                }
            //            }
            //        }
            //        break;
            //    case "RM350_OTP_Power"://otp power                10
            //        {
            //            RM350_0XFE_XX = 0x00;
            //            Send_buf[1] = 0x0B;
            //            Setting.TempRecord.RM69350CmdType = 0x02;
            //            Send_buf[3] = 0x04;
            //            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            //            try
            //            {
            //                MainForm.comPort.Write(Send_buf, 0, 7);
            //            }
            //            catch
            //            {
            //                MessageBox.Show("串口有误，请检查", "错误");
            //            }
            //            T1.Enabled = false;
            //            T1.Interval = 2000;
            //            T1.Enabled = true;

            //            Delay(100);

            //            if (radioButton13.Checked == true)
            //            {
            //                RM350_0XFE_XX = 0x00;
            //                if (Convert.ToDecimal(textBox68.Text) > 3)
            //                {
            //                    MessageBox.Show("OTP次数不能大于3次", "警告");
            //                    return;
            //                }
            //                else
            //                {
            //                    RM350_0XFE_XX = 0x00;
            //                    Send_buf[1] = 0x10;

            //                    CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            //                    try
            //                    {
            //                        MainForm.comPort.Write(Send_buf, 0, 7);
            //                    }
            //                    catch
            //                    {
            //                        MessageBox.Show("串口有误，请检查", "错误");
            //                    }
            //                    T1.Enabled = false;
            //                    T1.Interval = 2000;
            //                    T1.Enabled = true;
            //                }
            //            }
            //        }
            //        break;
            //    case "RM350_OTP_SPR"://otp power                10
            //        {
            //            RM350_0XFE_XX = 0x00;
            //            Send_buf[1] = 0x0B;
            //            Setting.TempRecord.RM69350CmdType = 0x02;
            //            Send_buf[3] = 0x00;
            //            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            //            try
            //            {
            //                MainForm.comPort.Write(Send_buf, 0, 7);
            //            }
            //            catch
            //            {
            //                MessageBox.Show("串口有误，请检查", "错误");
            //            }
            //            T1.Enabled = false;
            //            T1.Interval = 2000;
            //            T1.Enabled = true;

            //            Delay(100);

            //            if (radioButton17.Checked == true)
            //            {
            //                RM350_0XFE_XX = 0x00;
            //                if (Convert.ToDecimal(textBox68.Text) > 3)
            //                {
            //                    MessageBox.Show("OTP次数不能大于3次", "警告");
            //                    return;
            //                }
            //                else
            //                {
            //                    RM350_0XFE_XX = 0x00;
            //                    Send_buf[1] = 0x0A;

            //                    CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            //                    try
            //                    {
            //                        MainForm.comPort.Write(Send_buf, 0, 7);
            //                    }
            //                    catch
            //                    {
            //                        MessageBox.Show("串口有误，请检查", "错误");
            //                    }
            //                    T1.Enabled = false;
            //                    T1.Interval = 2000;
            //                    T1.Enabled = true;
            //                }
            //            }
            //            RM350_0XFE_XX = 0x00;
            //            Send_buf[1] = 0x07;
            //        }
            //        break;
            //}
        }

        private void btn310Stop_Click(object sender, EventArgs e)
        {
            //T12.Enabled = false;
            //flag1 = 1;
            //flag2 = 1;
            //flag3 = 1;
            //adjust_Rnum = 0;
            //adjust_Gnum = 0;
            //adjust_Bnum = 0;
            //adjust_l = 0;
            //adjust_x = 0;
            //adjust_y = 0;
            T5.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;
            //--------reset---------------
            Gamma_adjust_timeout_flag = false;
            Gamma_auto_adjust_count = 0;
            //----------------------------

        }



        private void radioButton12_CheckedChanged(object sender, EventArgs e)
        {
            RadioButton btnTmp = (RadioButton)sender;
            switch (btnTmp.Name)
            {
                case "RM69310_11GP":
                    Application.DoEvents();
                    rm69310_gammaR4.Enabled = false; rm69310_gammaG4.Enabled = false; rm69310_gammaB4.Enabled = false;
                    rm69310_gammaR5.Enabled = false; rm69310_gammaG5.Enabled = false; rm69310_gammaB5.Enabled = false;
                    rm69310_gammaR6.Enabled = false; rm69310_gammaG6.Enabled = false; rm69310_gammaB6.Enabled = false;
                    rm69310_gammaR8.Enabled = false; rm69310_gammaG8.Enabled = false; rm69310_gammaB8.Enabled = false;
                    rm69310_gammaR9.Enabled = false; rm69310_gammaG9.Enabled = false; rm69310_gammaB9.Enabled = false;
                    rm69310_gammaR10.Enabled = false; rm69310_gammaG10.Enabled = false; rm69310_gammaB10.Enabled = false;
                    rm69310_gammaR11.Enabled = false; rm69310_gammaG11.Enabled = false; rm69310_gammaB11.Enabled = false;
                    rm69310_gammaR13.Enabled = false; rm69310_gammaG13.Enabled = false; rm69310_gammaB13.Enabled = false;
                    rm69310_gammaR15.Enabled = false; rm69310_gammaG15.Enabled = false; rm69310_gammaB15.Enabled = false;
                    rm69310_gammaR17.Enabled = false; rm69310_gammaG17.Enabled = false; rm69310_gammaB17.Enabled = false;
                    rm69310_gammaR19.Enabled = false; rm69310_gammaG19.Enabled = false; rm69310_gammaB19.Enabled = false;
                    rm69310_gammaR21.Enabled = false; rm69310_gammaG21.Enabled = false; rm69310_gammaB21.Enabled = false;
                    rm69310_gammaR22.Enabled = false; rm69310_gammaG22.Enabled = false; rm69310_gammaB22.Enabled = false;
                    rm69310_gammaR23.Enabled = false; rm69310_gammaG23.Enabled = false; rm69310_gammaB23.Enabled = false;
                    break;

                case "RM69310_25GP":
                    rm69310_gammaR4.Enabled = true; rm69310_gammaG4.Enabled = true; rm69310_gammaB4.Enabled = true;
                    rm69310_gammaR5.Enabled = true; rm69310_gammaG5.Enabled = true; rm69310_gammaB5.Enabled = true;
                    rm69310_gammaR6.Enabled = true; rm69310_gammaG6.Enabled = true; rm69310_gammaB6.Enabled = true;
                    rm69310_gammaR8.Enabled = true; rm69310_gammaG8.Enabled = true; rm69310_gammaB8.Enabled = true;
                    rm69310_gammaR9.Enabled = true; rm69310_gammaG9.Enabled = true; rm69310_gammaB9.Enabled = true;
                    rm69310_gammaR10.Enabled = true; rm69310_gammaG10.Enabled = true; rm69310_gammaB10.Enabled = true;
                    rm69310_gammaR11.Enabled = true; rm69310_gammaG11.Enabled = true; rm69310_gammaB11.Enabled = true;
                    rm69310_gammaR13.Enabled = true; rm69310_gammaG13.Enabled = true; rm69310_gammaB13.Enabled = true;
                    rm69310_gammaR15.Enabled = true; rm69310_gammaG15.Enabled = true; rm69310_gammaB15.Enabled = true;
                    rm69310_gammaR17.Enabled = true; rm69310_gammaG17.Enabled = true; rm69310_gammaB17.Enabled = true;
                    rm69310_gammaR19.Enabled = true; rm69310_gammaG19.Enabled = true; rm69310_gammaB19.Enabled = true;
                    rm69310_gammaR21.Enabled = true; rm69310_gammaG21.Enabled = true; rm69310_gammaB21.Enabled = true;
                    rm69310_gammaR22.Enabled = true; rm69310_gammaG22.Enabled = true; rm69310_gammaB22.Enabled = true;
                    rm69310_gammaR23.Enabled = true; rm69310_gammaG23.Enabled = true; rm69310_gammaB23.Enabled = true;
                    break;

                default: break;
            }

        }

        private void Rm69310_VisibleChanged(object sender, EventArgs e)
        {
            if (this.Visible == true) //子窗体已开启标识
            {
                //MainForm.comPort.Close();
                //Thread.Sleep(5);
                MainForm.comPort.DataReceived -= new SerialDataReceivedEventHandler(MainForm.SerialPort1_DataReceived);

                Thread.Sleep(5);
                MainForm.comPort.DataReceived += new SerialDataReceivedEventHandler(SerialPort1_DataReceived);
                Thread.Sleep(5);
                //MainForm.comPort.Open();
                textBox1.Text += "com 已经连接！\r\n";
            }
            else
            {

            }
        }

        private void Rm69310_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = true;
            btnBack_Click(btnBack, e);
        }

        private void btnAutogamma310_Click(object sender, EventArgs e)
        {
            int flag = 1;
            double Lr_d, Lg_d, Lb_d;
            D_lums = new double[25, 3];
            D_lum = new double[25];
            index = 0;
            adjust_Rnum = 0;
            adjust_Gnum = 0;
            adjust_Bnum = 0;
            adjust_l = 0;
            adjust_x = 0;
            adjust_y = 0;

            //------------flexible gamma use------------------
            Gamma_auto_adjust_count = 0;
            Gamma_adjust_timeout_flag = false;
            //------------------------------------------------
            D_lum[0] = Convert.ToDouble(txt_350_255.Text);
            D_lum[1] = Convert.ToDouble(txt_350_239.Text);
            D_lum[2] = Convert.ToDouble(txt_350_223.Text);
            D_lum[3] = Convert.ToDouble(txt_350_207.Text);
            D_lum[4] = Convert.ToDouble(txt_350_191.Text);
            D_lum[5] = Convert.ToDouble(txt_350_175.Text);
            D_lum[6] = Convert.ToDouble(txt_350_159.Text);
            D_lum[7] = Convert.ToDouble(txt_350_143.Text);
            D_lum[8] = Convert.ToDouble(txt_350_127.Text);
            D_lum[9] = Convert.ToDouble(txt_350_111.Text);
            D_lum[10] = Convert.ToDouble(txt_350_95.Text);
            D_lum[11] = Convert.ToDouble(txt_350_79.Text);
            D_lum[12] = Convert.ToDouble(txt_350_63.Text);
            D_lum[13] = Convert.ToDouble(txt_350_55.Text);
            D_lum[14] = Convert.ToDouble(txt_350_47.Text);
            D_lum[15] = Convert.ToDouble(txt_350_39.Text);
            D_lum[16] = Convert.ToDouble(txt_350_35.Text);
            D_lum[17] = Convert.ToDouble(txt_350_31.Text);
            D_lum[18] = Convert.ToDouble(txt_350_27.Text);
            D_lum[19] = Convert.ToDouble(txt_350_23.Text);
            D_lum[20] = Convert.ToDouble(txt_350_19.Text);
            D_lum[21] = Convert.ToDouble(txt_350_15.Text);
            D_lum[22] = Convert.ToDouble(txt_350_11.Text);
            D_lum[23] = Convert.ToDouble(txt_350_7.Text);
            D_lum[24] = Convert.ToDouble(txt_350_1.Text);


            D_x = Convert.ToDouble(textx.Text);
            D_y = Convert.ToDouble(texty.Text);
            Lr_d = R_y * ((D_y - G_y) * (B_x - D_x) + (D_x - G_x) * (D_y - B_y)) / B_y / ((D_y - G_y) * (D_x - R_x) + (D_x - G_x) * (R_y - D_y));
            Lb_d = 1.0;
            Lg_d = G_y * ((D_y - R_y) * (B_x - D_x) + (D_x - R_x) * (D_y - B_y)) / B_y / ((D_y - R_y) * (D_x - G_x) + (D_x - R_x) * (G_y - D_y));

            for (int i = 0; i < 25; i++)
            {
                D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
            }

            //if (MainForm.IC_comboBox.SelectedIndex == Setting.IC_Type.RM67195)//195
            //{
            //    gamma_r = GAMMAs.RM195_Gamma_r;
            //    gamma_g = GAMMAs.RM195_Gamma_g;
            //    gamma_b = GAMMAs.RM195_Gamma_b;
            //}
            //else//295 and 198
            //{


            //RM69310_Gamma_Click(RM69310_GammaR_all, null);
            //for (int i = 0; i < 25; i++)
            //{
            //    GAMMAs.RM69310_Gamma_r[i] = (ushort)GAMMA_69310_Point25_BufdataR[i];
            //    GAMMAs.RM69310_Gamma_g[i] = (ushort)GAMMA_69310_Point25_BufdataG[i];
            //    GAMMAs.RM69310_Gamma_b[i] = (ushort)GAMMA_69310_Point25_BufdataB[i];
            //}

            gamma_r = GAMMAs.RM69310_Gamma_r;
            gamma_g = GAMMAs.RM69310_Gamma_g;
            gamma_b = GAMMAs.RM69310_Gamma_b;
            //}

            //txtRM67195Add.Text = "FE";
            //if (MainForm.IC_comboBox.SelectedIndex == Setting.IC_Type.RM67198)
            //{
            //    if (RM67198_rbt_HBM.Checked == true)
            //    {
            //        txtRM67195Data.Text = "51";
            //    }
            //    else
            //    {
            //        txtRM67195Data.Text = "50";
            //    }
            //}
            //else if (MainForm.IC_comboBox.SelectedIndex == Setting.IC_Type.RM69298)
            //{
            //    if (RM67198_rbt_HBM.Checked == true)
            //    {
            //        txtRM67195Data.Text = "52";
            //    }
            //    else
            //    {
            //        txtRM67195Data.Text = "50";
            //    }

            //}
            //else
            //    txtRM67195Data.Text = "05";
            //RM196_Others_Click(RM67195_RegW, e);


            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        lum_off = Convert.ToDouble(textlum_off.Text);
                        x_off = Convert.ToDouble(textx_off.Text);
                        y_off = Convert.ToDouble(texty_off.Text);
                        OLED_Show_Click(btnwhite, e);
                        T5.Enabled = true;
                    }
                    break;
                case 2://寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }

        private void T5_Tick(object sender, EventArgs e)
        {
            double Lr_t, Lg_t, Lb_t, lumr = 0, lumg = 0, lumb = 0;
            Msr_CA310();

            if ((auto_lum < (D_lum[index] + lum_off)) && (auto_lum > (D_lum[index] - lum_off)))//G
            {
                flag1 = 0;
            }
            if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//B
            {
                flag2 = 0;
            }
            if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//R
            {
                flag3 = 0;
            }

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T5.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                Gamma_auto_adjust_count = 0;
                //--------
                index++;
                adjust_Rnum = 0;
                adjust_Gnum = 0;
                adjust_Bnum = 0;
                adjust_l = 0;
                adjust_x = 0;
                adjust_y = 0;
                if (index == 25)
                {
                    //        RM67195_GAMMA_Click(RM67195_GammaW_Red, null);
                    //        RM67195_GAMMA_Click(RM67195_GammaW_Green, null);
                    //        RM67195_GAMMA_Click(RM67195_GammaW_Blue, null);
                    RM69310_Gamma_Click(RM69310_GammaW_all, null);
                    textBox1.Text = textBox1.Text + "RM67195/295/198 gamma调试完毕!\r\n";
                }
                if (index <= 24)
                {
                    switch (index)
                    {
                        case 1:
                            lum_off = Convert.ToDouble(txt_350_239off.Text);
                            break;
                        case 2:
                            lum_off = Convert.ToDouble(txt_350_223off.Text);
                            break;
                        case 3:
                            lum_off = Convert.ToDouble(txt_350_207off.Text);
                            break;
                        case 4:
                            lum_off = Convert.ToDouble(txt_350_191off.Text);
                            break;
                        case 5:
                            lum_off = Convert.ToDouble(txt_350_175off.Text);
                            break;
                        case 6:
                            lum_off = Convert.ToDouble(txt_350_159off.Text);
                            break;
                        case 7:
                            lum_off = Convert.ToDouble(txt_350_143off.Text);
                            break;
                        case 8:
                            lum_off = Convert.ToDouble(txt_350_127off.Text) * Convert.ToDouble(txt_350_127.Text) / 100;
                            break;
                        case 9:
                            lum_off = Convert.ToDouble(txt_350_111off.Text) * Convert.ToDouble(txt_350_111.Text) / 100;
                            break;
                        case 10:
                            lum_off = Convert.ToDouble(txt_350_95off.Text) * Convert.ToDouble(txt_350_95.Text) / 100;
                            break;
                        case 11:
                            lum_off = Convert.ToDouble(txt_350_79off.Text) * Convert.ToDouble(txt_350_79.Text) / 100;
                            break;
                        case 12:
                            lum_off = Convert.ToDouble(txt_350_63off.Text) * Convert.ToDouble(txt_350_63.Text) / 100;
                            break;
                        case 13:
                            lum_off = Convert.ToDouble(txt_350_55off.Text) * Convert.ToDouble(txt_350_55.Text) / 100;
                            break;
                        case 14:
                            lum_off = Convert.ToDouble(txt_350_47off.Text) * Convert.ToDouble(txt_350_47.Text) / 100;
                            break;
                        case 15:
                            lum_off = Convert.ToDouble(txt_350_39off.Text) * Convert.ToDouble(txt_350_39.Text) / 100;
                            break;
                        case 16:
                            lum_off = Convert.ToDouble(txt_350_35off.Text) * Convert.ToDouble(txt_350_35off.Text) / 100;
                            break;
                        case 17:
                            lum_off = Convert.ToDouble(txt_350_31off.Text) * Convert.ToDouble(txt_350_31off.Text) / 100;
                            break;
                        case 18:
                            lum_off = Convert.ToDouble(txt_350_27off.Text) * Convert.ToDouble(txt_350_27off.Text) / 100;
                            break;
                        case 19:
                            lum_off = Convert.ToDouble(txt_350_23off.Text) * Convert.ToDouble(txt_350_23off.Text) / 100;
                            break;
                        case 20:
                            lum_off = Convert.ToDouble(txt_350_19off.Text) * Convert.ToDouble(txt_350_19off.Text) / 100;
                            break;
                        case 21:
                            lum_off = Convert.ToDouble(txt_350_15off.Text) * Convert.ToDouble(txt_350_15off.Text) / 100;
                            break;
                        case 22:
                            lum_off = Convert.ToDouble(txt_350_11off.Text) * Convert.ToDouble(txt_350_11off.Text) / 100;
                            break;
                        case 23:
                            lum_off = Convert.ToDouble(txt_350_7off.Text) * Convert.ToDouble(txt_350_7off.Text) / 100;
                            break;
                        case 24:
                            lum_off = Convert.ToDouble(txt_350_1off.Text) * Convert.ToDouble(txt_350_1off.Text) / 100;
                            break;


                    }

                    numPatternR.Value = Parttern_RM69310[index];
                    numPatternG.Value = Parttern_RM69310[index];
                    numPatternB.Value = Parttern_RM69310[index];
                    OLED_Show_Click(btnPatternW, e);//写入Pattern参数
                    Thread.Sleep(25); //1秒=1000毫秒，
                    T5.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {
                decimal r, g, b;
                int flexible_gamma_count = 100;

                Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
                Lb_t = 1.0;
                Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
                lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);

                try
                {
                    switch (index)
                    {
                        case 0:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 50, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR24.Value, rm69310_gammaG24.Value, rm69310_gammaB24.Value, 255, 255, 255, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR24.Value = r;
                                rm69310_gammaG24.Value = g;
                                rm69310_gammaB24.Value = b;
                                auto_gamma_write(gamma_r[24], Convert.ToUInt16(rm69310_gammaR24.Value), gamma_g[24], Convert.ToUInt16(rm69310_gammaG24.Value), gamma_b[24], Convert.ToUInt16(rm69310_gammaB24.Value));
                            }
                            break;
                        case 1:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 50, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR23.Value, rm69310_gammaG23.Value, rm69310_gammaB23.Value, 255, 255, 239, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, 0x3ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR23.Value = r;
                                rm69310_gammaG23.Value = g;
                                rm69310_gammaB23.Value = b;
                                auto_gamma_write(gamma_r[23], Convert.ToUInt16(rm69310_gammaR23.Value), gamma_g[23], Convert.ToUInt16(rm69310_gammaG23.Value), gamma_b[23], Convert.ToUInt16(rm69310_gammaB23.Value));

                            }
                            break;
                        case 2:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 50, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR22.Value, rm69310_gammaG22.Value, rm69310_gammaB22.Value, 255, 239, 223, rm69310_gammaR24.Value, rm69310_gammaG24.Value, rm69310_gammaB24.Value, rm69310_gammaR23.Value, rm69310_gammaG23.Value, rm69310_gammaB23.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR22.Value = r;
                                rm69310_gammaG22.Value = g;
                                rm69310_gammaB22.Value = b;
                                auto_gamma_write(gamma_r[22], Convert.ToUInt16(rm69310_gammaR22.Value), gamma_g[22], Convert.ToUInt16(rm69310_gammaG22.Value), gamma_b[22], Convert.ToUInt16(rm69310_gammaB22.Value));

                            }
                            break;
                        case 3:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR21.Value, rm69310_gammaG21.Value, rm69310_gammaB21.Value, 239, 223, 207, rm69310_gammaR23.Value, rm69310_gammaG23.Value, rm69310_gammaB23.Value, rm69310_gammaR22.Value, rm69310_gammaG22.Value, rm69310_gammaB22.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR21.Value = r;
                                rm69310_gammaG21.Value = g;
                                rm69310_gammaB21.Value = b;
                                auto_gamma_write(gamma_r[21], Convert.ToUInt16(rm69310_gammaR21.Value), gamma_g[21], Convert.ToUInt16(rm69310_gammaG21.Value), gamma_b[21], Convert.ToUInt16(rm69310_gammaB21.Value));

                            }
                            break;
                        case 4:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR20.Value, rm69310_gammaG20.Value, rm69310_gammaB20.Value, 223, 207, 191, rm69310_gammaR22.Value, rm69310_gammaG22.Value, rm69310_gammaB22.Value, rm69310_gammaR21.Value, rm69310_gammaG21.Value, rm69310_gammaB21.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR20.Value = r;
                                rm69310_gammaG20.Value = g;
                                rm69310_gammaB20.Value = b;
                                auto_gamma_write(gamma_r[20], Convert.ToUInt16(rm69310_gammaR20.Value), gamma_g[20], Convert.ToUInt16(rm69310_gammaG20.Value), gamma_b[20], Convert.ToUInt16(rm69310_gammaB20.Value));

                            }
                            break;
                        case 5:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR19.Value, rm69310_gammaG19.Value, rm69310_gammaB19.Value, 207, 191, 175, rm69310_gammaR21.Value, rm69310_gammaG21.Value, rm69310_gammaB21.Value, rm69310_gammaR20.Value, rm69310_gammaG20.Value, rm69310_gammaB20.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR19.Value = r;
                                rm69310_gammaG19.Value = g;
                                rm69310_gammaB19.Value = b;
                                auto_gamma_write(gamma_r[19], Convert.ToUInt16(rm69310_gammaR19.Value), gamma_g[19], Convert.ToUInt16(rm69310_gammaG19.Value), gamma_b[19], Convert.ToUInt16(rm69310_gammaB19.Value));


                            }
                            break;
                        case 6:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR18.Value, rm69310_gammaG18.Value, rm69310_gammaB18.Value, 191, 175, 159, rm69310_gammaR20.Value, rm69310_gammaG20.Value, rm69310_gammaB20.Value, rm69310_gammaR19.Value, rm69310_gammaG19.Value, rm69310_gammaB19.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR18.Value = r;
                                rm69310_gammaG18.Value = g;
                                rm69310_gammaB18.Value = b;
                                auto_gamma_write(gamma_r[18], Convert.ToUInt16(rm69310_gammaR18.Value), gamma_g[18], Convert.ToUInt16(rm69310_gammaG18.Value), gamma_b[18], Convert.ToUInt16(rm69310_gammaB18.Value));

                            }
                            break;
                        case 7:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR17.Value, rm69310_gammaG17.Value, rm69310_gammaB17.Value, 175, 159, 143, rm69310_gammaR19.Value, rm69310_gammaG19.Value, rm69310_gammaB19.Value, rm69310_gammaR18.Value, rm69310_gammaG18.Value, rm69310_gammaB18.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR17.Value = r;
                                rm69310_gammaG17.Value = g;
                                rm69310_gammaB17.Value = b;
                                auto_gamma_write(gamma_r[17], Convert.ToUInt16(rm69310_gammaR17.Value), gamma_g[17], Convert.ToUInt16(rm69310_gammaG17.Value), gamma_b[17], Convert.ToUInt16(rm69310_gammaB17.Value));

                            }
                            break;
                        case 8:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR16.Value, rm69310_gammaG16.Value, rm69310_gammaB16.Value, 159, 143, 127, rm69310_gammaR18.Value, rm69310_gammaG18.Value, rm69310_gammaB18.Value, rm69310_gammaR17.Value, rm69310_gammaG17.Value, rm69310_gammaB17.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR16.Value = r;
                                rm69310_gammaG16.Value = g;
                                rm69310_gammaB16.Value = b;
                                auto_gamma_write(gamma_r[16], Convert.ToUInt16(rm69310_gammaR16.Value), gamma_g[16], Convert.ToUInt16(rm69310_gammaG16.Value), gamma_b[16], Convert.ToUInt16(rm69310_gammaB16.Value));

                            }
                            break;
                        case 9:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR15.Value, rm69310_gammaG15.Value, rm69310_gammaB15.Value, 143, 127, 111, rm69310_gammaR17.Value, rm69310_gammaG17.Value, rm69310_gammaB17.Value, rm69310_gammaR16.Value, rm69310_gammaG16.Value, rm69310_gammaB16.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR15.Value = r;
                                rm69310_gammaG15.Value = g;
                                rm69310_gammaB15.Value = b;
                                auto_gamma_write(gamma_r[15], Convert.ToUInt16(rm69310_gammaR15.Value), gamma_g[15], Convert.ToUInt16(rm69310_gammaG15.Value), gamma_b[15], Convert.ToUInt16(rm69310_gammaB15.Value));

                            }
                            break;
                        case 10:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR14.Value, rm69310_gammaG14.Value, rm69310_gammaB14.Value, 127, 111, 95, rm69310_gammaR16.Value, rm69310_gammaG16.Value, rm69310_gammaB16.Value, rm69310_gammaR15.Value, rm69310_gammaG15.Value, rm69310_gammaB15.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR14.Value = r;
                                rm69310_gammaG14.Value = g;
                                rm69310_gammaB14.Value = b;
                                auto_gamma_write(gamma_r[14], Convert.ToUInt16(rm69310_gammaR14.Value), gamma_g[14], Convert.ToUInt16(rm69310_gammaG14.Value), gamma_b[14], Convert.ToUInt16(rm69310_gammaB14.Value));

                            }
                            break;
                        case 11:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR13.Value, rm69310_gammaG13.Value, rm69310_gammaB13.Value, 111, 95, 79, rm69310_gammaR15.Value, rm69310_gammaG15.Value, rm69310_gammaB15.Value, rm69310_gammaR14.Value, rm69310_gammaG14.Value, rm69310_gammaB14.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR13.Value = r;
                                rm69310_gammaG13.Value = g;
                                rm69310_gammaB13.Value = b;
                                auto_gamma_write(gamma_r[13], Convert.ToUInt16(rm69310_gammaR13.Value), gamma_g[13], Convert.ToUInt16(rm69310_gammaG13.Value), gamma_b[13], Convert.ToUInt16(rm69310_gammaB13.Value));

                            }
                            break;
                        case 12:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR12.Value, rm69310_gammaG12.Value, rm69310_gammaB12.Value, 95, 79, 63, rm69310_gammaR14.Value, rm69310_gammaG14.Value, rm69310_gammaB14.Value, rm69310_gammaR13.Value, rm69310_gammaG13.Value, rm69310_gammaB13.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR12.Value = r;
                                rm69310_gammaG12.Value = g;
                                rm69310_gammaB12.Value = b;
                                auto_gamma_write(gamma_r[12], Convert.ToUInt16(rm69310_gammaR12.Value), gamma_g[12], Convert.ToUInt16(rm69310_gammaG12.Value), gamma_b[12], Convert.ToUInt16(rm69310_gammaB12.Value));

                            }
                            break;
                        case 13:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR11.Value, rm69310_gammaG11.Value, rm69310_gammaB11.Value, 79, 63, 55, rm69310_gammaR13.Value, rm69310_gammaG13.Value, rm69310_gammaB13.Value, rm69310_gammaR12.Value, rm69310_gammaG12.Value, rm69310_gammaB12.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR11.Value = r;
                                rm69310_gammaG11.Value = g;
                                rm69310_gammaB11.Value = b;
                                auto_gamma_write(gamma_r[11], Convert.ToUInt16(rm69310_gammaR11.Value), gamma_g[11], Convert.ToUInt16(rm69310_gammaG11.Value), gamma_b[11], Convert.ToUInt16(rm69310_gammaB11.Value));

                            }
                            break;
                        case 14:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR10.Value, rm69310_gammaG10.Value, rm69310_gammaB10.Value, 63, 55, 47, rm69310_gammaR12.Value, rm69310_gammaG12.Value, rm69310_gammaB12.Value, rm69310_gammaR11.Value, rm69310_gammaG11.Value, rm69310_gammaB11.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR10.Value = r;
                                rm69310_gammaG10.Value = g;
                                rm69310_gammaB10.Value = b;
                                auto_gamma_write(gamma_r[10], Convert.ToUInt16(rm69310_gammaR10.Value), gamma_g[10], Convert.ToUInt16(rm69310_gammaG10.Value), gamma_b[10], Convert.ToUInt16(rm69310_gammaB10.Value));

                            }
                            break;
                        case 15:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR9.Value, rm69310_gammaG9.Value, rm69310_gammaB9.Value, 55, 47, 39, rm69310_gammaR11.Value, rm69310_gammaG11.Value, rm69310_gammaB11.Value, rm69310_gammaR10.Value, rm69310_gammaG10.Value, rm69310_gammaB10.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR9.Value = r;
                                rm69310_gammaG9.Value = g;
                                rm69310_gammaB9.Value = b;
                                auto_gamma_write(gamma_r[9], Convert.ToUInt16(rm69310_gammaR9.Value), gamma_g[9], Convert.ToUInt16(rm69310_gammaG9.Value), gamma_b[9], Convert.ToUInt16(rm69310_gammaB9.Value));

                            }
                            break;
                        case 16:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 4, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR8.Value, rm69310_gammaG8.Value, rm69310_gammaB8.Value, 47, 39, 35, rm69310_gammaR10.Value, rm69310_gammaG10.Value, rm69310_gammaB10.Value, rm69310_gammaR9.Value, rm69310_gammaG9.Value, rm69310_gammaB9.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR8.Value = r;
                                rm69310_gammaG8.Value = g;
                                rm69310_gammaB8.Value = b;
                                auto_gamma_write(gamma_r[8], Convert.ToUInt16(rm69310_gammaR8.Value), gamma_g[8], Convert.ToUInt16(rm69310_gammaG8.Value), gamma_b[8], Convert.ToUInt16(rm69310_gammaB8.Value));

                            }
                            break;
                        case 17:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 4, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR7.Value, rm69310_gammaG7.Value, rm69310_gammaB7.Value, 39, 35, 31, rm69310_gammaR9.Value, rm69310_gammaG9.Value, rm69310_gammaB9.Value, rm69310_gammaR8.Value, rm69310_gammaG8.Value, rm69310_gammaB8.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR7.Value = r;
                                rm69310_gammaG7.Value = g;
                                rm69310_gammaB7.Value = b;
                                auto_gamma_write(gamma_r[7], Convert.ToUInt16(rm69310_gammaR7.Value), gamma_g[7], Convert.ToUInt16(rm69310_gammaG7.Value), gamma_b[7], Convert.ToUInt16(rm69310_gammaB7.Value));

                            }
                            break;
                        case 18:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR6.Value, rm69310_gammaG6.Value, rm69310_gammaB6.Value, 35, 31, 27, rm69310_gammaR8.Value, rm69310_gammaG8.Value, rm69310_gammaB8.Value, rm69310_gammaR7.Value, rm69310_gammaG7.Value, rm69310_gammaB7.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR6.Value = r;
                                rm69310_gammaG6.Value = g;
                                rm69310_gammaB6.Value = b;
                                auto_gamma_write(gamma_r[6], Convert.ToUInt16(rm69310_gammaR6.Value), gamma_g[6], Convert.ToUInt16(rm69310_gammaG6.Value), gamma_b[6], Convert.ToUInt16(rm69310_gammaB6.Value));

                            }
                            break;
                        case 19:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 20, 3, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR5.Value, rm69310_gammaG5.Value, rm69310_gammaB5.Value, 31, 27, 23, rm69310_gammaR7.Value, rm69310_gammaG7.Value, rm69310_gammaB7.Value, rm69310_gammaR6.Value, rm69310_gammaG6.Value, rm69310_gammaB6.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR5.Value = r;
                                rm69310_gammaG5.Value = g;
                                rm69310_gammaB5.Value = b;
                                auto_gamma_write(gamma_r[5], Convert.ToUInt16(rm69310_gammaR5.Value), gamma_g[5], Convert.ToUInt16(rm69310_gammaG5.Value), gamma_b[5], Convert.ToUInt16(rm69310_gammaB5.Value));

                            }
                            break;
                        case 20:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 20, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR4.Value, rm69310_gammaG4.Value, rm69310_gammaB4.Value, 27, 23, 19, rm69310_gammaR6.Value, rm69310_gammaG6.Value, rm69310_gammaB6.Value, rm69310_gammaR5.Value, rm69310_gammaG5.Value, rm69310_gammaB5.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR4.Value = r;
                                rm69310_gammaG4.Value = g;
                                rm69310_gammaB4.Value = b;
                                auto_gamma_write(gamma_r[4], Convert.ToUInt16(rm69310_gammaR4.Value), gamma_g[4], Convert.ToUInt16(rm69310_gammaG4.Value), gamma_b[4], Convert.ToUInt16(rm69310_gammaB4.Value));

                            }
                            break;
                        case 21:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR3.Value, rm69310_gammaG3.Value, rm69310_gammaB3.Value, 23, 19, 15, rm69310_gammaR5.Value, rm69310_gammaG5.Value, rm69310_gammaB5.Value, rm69310_gammaR4.Value, rm69310_gammaG4.Value, rm69310_gammaB4.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR3.Value = r;
                                rm69310_gammaG3.Value = g;
                                rm69310_gammaB3.Value = b;
                                auto_gamma_write(gamma_r[3], Convert.ToUInt16(rm69310_gammaR3.Value), gamma_g[3], Convert.ToUInt16(rm69310_gammaG3.Value), gamma_b[3], Convert.ToUInt16(rm69310_gammaB3.Value));

                            }
                            break;
                        case 22:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR2.Value, rm69310_gammaG2.Value, rm69310_gammaB2.Value, 19, 15, 11, rm69310_gammaR4.Value, rm69310_gammaG4.Value, rm69310_gammaB4.Value, rm69310_gammaR3.Value, rm69310_gammaG3.Value, rm69310_gammaB3.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR2.Value = r;
                                rm69310_gammaG2.Value = g;
                                rm69310_gammaB2.Value = b;
                                auto_gamma_write(gamma_r[2], Convert.ToUInt16(rm69310_gammaR2.Value), gamma_g[2], Convert.ToUInt16(rm69310_gammaG2.Value), gamma_b[2], Convert.ToUInt16(rm69310_gammaB2.Value));

                            }
                            break;
                        case 23:
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 5, 1, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR1.Value, rm69310_gammaG1.Value, rm69310_gammaB1.Value, 15, 11, 7, rm69310_gammaR3.Value, rm69310_gammaG3.Value, rm69310_gammaB3.Value, rm69310_gammaR2.Value, rm69310_gammaG2.Value, rm69310_gammaB2.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR1.Value = r;
                                rm69310_gammaG1.Value = g;
                                rm69310_gammaB1.Value = b;
                                auto_gamma_write(gamma_r[1], Convert.ToUInt16(rm69310_gammaR1.Value), gamma_g[1], Convert.ToUInt16(rm69310_gammaG1.Value), gamma_b[1], Convert.ToUInt16(rm69310_gammaB1.Value));

                            }
                            break;
                        case 24://阶以下
                            {
                                R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 5, 1, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, rm69310_gammaR0.Value, rm69310_gammaG0.Value, rm69310_gammaB0.Value, 11, 7, 0, rm69310_gammaR2.Value, rm69310_gammaG2.Value, rm69310_gammaB2.Value, rm69310_gammaR1.Value, rm69310_gammaG1.Value, rm69310_gammaB1.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm69310_gammaR0.Value = r;
                                rm69310_gammaG0.Value = g;
                                rm69310_gammaB0.Value = b;
                                auto_gamma_write(gamma_r[0], Convert.ToUInt16(rm69310_gammaR0.Value), gamma_g[0], Convert.ToUInt16(rm69310_gammaG0.Value), gamma_b[0], Convert.ToUInt16(rm69310_gammaB0.Value));

                            }
                            break;



                        default:
                            break;
                    }
                }
                catch
                {
                    T5.Enabled = false;

                    MessageBox.Show("数值超限！");
                }
            }
        }



        //private void T12_Tick(object sender, EventArgs e)
        //{
        //    double Lr_t, Lg_t, Lb_t, lumr = 0, lumg = 0, lumb = 0;
        //    Msr_CA310();

        //    if ((auto_lum < (D_lum[index] + lum_off)) && (auto_lum > (D_lum[index] - lum_off)))//G
        //    {
        //        flag1 = 0;
        //    }
        //    if ((auto_y < (D_y + y_off)) && (auto_y > (D_y - y_off)) && flag1 == 0)//B
        //    {
        //        flag2 = 0;
        //    }
        //    if ((auto_x < (D_x + x_off)) && (auto_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//R
        //    {
        //        flag3 = 0;
        //    }

        //    if (flag1 == 0 && flag2 == 0 && flag3 == 0)
        //    {
        //        T5.Enabled = false;
        //        //reset-----
        //        Gamma_adjust_timeout_flag = false;
        //        Gamma_auto_adjust_count = 0;
        //        //--------
        //        index++;
        //        adjust_Rnum = 0;
        //        adjust_Gnum = 0;
        //        adjust_Bnum = 0;
        //        adjust_l = 0;
        //        adjust_x = 0;
        //        adjust_y = 0;
        //        if (index == 30)
        //        {
        //            RM67195_GAMMA_Click(RM67195_GammaW_Red, null);
        //            RM67195_GAMMA_Click(RM67195_GammaW_Green, null);
        //            RM67195_GAMMA_Click(RM67195_GammaW_Blue, null);
        //            textBox1.Text = textBox1.Text + "RM67195/295/198 gamma调试完毕!\r\n";
        //        }
        //        if (index <= 29)
        //        {
        //            switch (index)
        //            {
        //                case 1:
        //                    lum_off = Convert.ToDouble(text195_254_off.Text);
        //                    break;
        //                case 2:
        //                    lum_off = Convert.ToDouble(text195_252_off.Text);
        //                    break;
        //                case 3:
        //                    lum_off = Convert.ToDouble(text195_248_off.Text);
        //                    break;
        //                case 4:
        //                    lum_off = Convert.ToDouble(text195_244_off.Text);
        //                    break;
        //                case 5:
        //                    lum_off = Convert.ToDouble(text195_236_off.Text);
        //                    break;
        //                case 6:
        //                    lum_off = Convert.ToDouble(text195_228_off.Text);
        //                    break;
        //                case 7:
        //                    lum_off = Convert.ToDouble(text195_220_off.Text);
        //                    break;
        //                case 8:
        //                    lum_off = Convert.ToDouble(text195_212_off.Text);
        //                    break;
        //                case 9:
        //                    lum_off = Convert.ToDouble(text195_204_off.Text);
        //                    break;
        //                case 10:
        //                    lum_off = Convert.ToDouble(text195_196_off.Text);
        //                    break;
        //                case 11:
        //                    lum_off = Convert.ToDouble(text195_188_off.Text);
        //                    break;
        //                case 12:
        //                    lum_off = Convert.ToDouble(text195_180_off.Text);
        //                    break;
        //                case 13:
        //                    lum_off = Convert.ToDouble(text195_164_off.Text);
        //                    break;
        //                case 14:
        //                    lum_off = Convert.ToDouble(text195_148_off.Text);
        //                    break;
        //                case 15:
        //                    lum_off = Convert.ToDouble(text195_132_off.Text) * Convert.ToDouble(text195_132.Text) / 100;
        //                    break;
        //                case 16:
        //                    lum_off = Convert.ToDouble(text195_116_off.Text) * Convert.ToDouble(text195_116.Text) / 100;
        //                    break;
        //                case 17:
        //                    lum_off = Convert.ToDouble(text195_100_off.Text) * Convert.ToDouble(text195_100.Text) / 100;
        //                    break;
        //                case 18:
        //                    lum_off = Convert.ToDouble(text195_84_off.Text) * Convert.ToDouble(text195_84.Text) / 100;
        //                    break;
        //                case 19:
        //                    lum_off = Convert.ToDouble(text195_68_off.Text) * Convert.ToDouble(text195_68.Text) / 100;
        //                    break;
        //                case 20:
        //                    lum_off = Convert.ToDouble(text195_52_off.Text) * Convert.ToDouble(text195_52.Text) / 100;
        //                    break;
        //                case 21:
        //                    lum_off = Convert.ToDouble(text195_44_off.Text) * Convert.ToDouble(text195_44.Text) / 100;
        //                    break;
        //                case 22:
        //                    lum_off = Convert.ToDouble(text195_36_off.Text) * Convert.ToDouble(text195_36.Text) / 100;
        //                    break;
        //                case 23:
        //                    lum_off = Convert.ToDouble(text195_28_off.Text) * Convert.ToDouble(text195_28.Text) / 100;
        //                    break;
        //                case 24:
        //                    lum_off = Convert.ToDouble(text195_20_off.Text) * Convert.ToDouble(text195_20.Text) / 100;
        //                    break;
        //                case 25:
        //                    lum_off = Convert.ToDouble(text195_16_off.Text) * Convert.ToDouble(text195_16.Text) / 100;
        //                    break;
        //                case 26:
        //                    lum_off = Convert.ToDouble(text195_12_off.Text) * Convert.ToDouble(text195_12.Text) / 100;
        //                    break;
        //                case 27:
        //                    lum_off = Convert.ToDouble(text195_8_off.Text) * Convert.ToDouble(text195_8.Text) / 100;
        //                    break;
        //                case 28:
        //                    lum_off = Convert.ToDouble(text195_4_off.Text) * Convert.ToDouble(text195_4.Text) / 100;
        //                    break;
        //                case 29:
        //                    lum_off = Convert.ToDouble(text195_3_off.Text) * Convert.ToDouble(text195_3.Text) / 100;
        //                    break;
        //            }

        //            numPatternR.Value = Parttern_RM67195[index];
        //            numPatternG.Value = Parttern_RM67195[index];
        //            numPatternB.Value = Parttern_RM67195[index];
        //            OLED_Show_Click(btnPatternW, e);//写入Pattern参数
        //            Thread.Sleep(15); //1秒=1000毫秒，
        //            T5.Enabled = true;
        //            flag1 = 1;
        //            flag2 = 1;
        //            flag3 = 1;
        //        }
        //    }
        //    else
        //    {
        //        decimal r, g, b;
        //        int flexible_gamma_count = 100;

        //        Lr_t = R_y * ((auto_y - G_y) * (B_x - auto_x) + (auto_x - G_x) * (auto_y - B_y)) / B_y / ((auto_y - G_y) * (auto_x - R_x) + (auto_x - G_x) * (R_y - auto_y));
        //        Lb_t = 1.0;
        //        Lg_t = G_y * ((auto_y - R_y) * (B_x - auto_x) + (auto_x - R_x) * (auto_y - B_y)) / B_y / ((auto_y - R_y) * (auto_x - G_x) + (auto_x - R_x) * (G_y - auto_y));
        //        lumr = auto_lum * Lr_t / (Lr_t + Lg_t + Lb_t);
        //        lumg = auto_lum * Lg_t / (Lr_t + Lg_t + Lb_t);
        //        lumb = auto_lum * Lb_t / (Lr_t + Lg_t + Lb_t);

        //        try
        //        {
        //            switch (index)
        //            {
        //                case 0:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 50, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR255.Value, NoRMG255.Value, NoRMB255.Value, 255, 255, 255, 0x7ff, 0x7ff, 0x7ff, 0x7ff, 0x7ff, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR255.Value = r;
        //                        NoRMG255.Value = g;
        //                        NoRMB255.Value = b;
        //                        auto_gamma_write(gamma_r[30], Convert.ToUInt16(NoRMR255.Value), gamma_g[30], Convert.ToUInt16(NoRMG255.Value), gamma_b[30], Convert.ToUInt16(NoRMB255.Value));
        //                    }
        //                    break;
        //                case 1:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 50, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR254.Value, NoRMG254.Value, NoRMB254.Value, 255, 255, 255, 0x7ff, 0x7ff, 0x7ff, 0x7ff, 0x7ff, 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR254.Value = r;
        //                        NoRMG254.Value = g;
        //                        NoRMB254.Value = b;
        //                        auto_gamma_write(gamma_r[29], Convert.ToUInt16(NoRMR254.Value), gamma_g[29], Convert.ToUInt16(NoRMG254.Value), gamma_b[29], Convert.ToUInt16(NoRMB254.Value));

        //                    }
        //                    break;
        //                case 2:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 50, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR252.Value, NoRMG252.Value, NoRMB252.Value, 255, 254, 252, NoRMR255.Value, NoRMG255.Value, NoRMB255.Value, NoRMR254.Value, NoRMG254.Value, NoRMB254.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR252.Value = r;
        //                        NoRMG252.Value = g;
        //                        NoRMB252.Value = b;
        //                        auto_gamma_write(gamma_r[28], Convert.ToUInt16(NoRMR252.Value), gamma_g[28], Convert.ToUInt16(NoRMG252.Value), gamma_b[28], Convert.ToUInt16(NoRMB252.Value));

        //                    }
        //                    break;
        //                case 3:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR248.Value, NoRMG248.Value, NoRMB248.Value, 254, 252, 248, NoRMR254.Value, NoRMG254.Value, NoRMB254.Value, NoRMR252.Value, NoRMG252.Value, NoRMB252.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR248.Value = r;
        //                        NoRMG248.Value = g;
        //                        NoRMB248.Value = b;
        //                        auto_gamma_write(gamma_r[27], Convert.ToUInt16(NoRMR248.Value), gamma_g[27], Convert.ToUInt16(NoRMG248.Value), gamma_b[27], Convert.ToUInt16(NoRMB248.Value));

        //                    }
        //                    break;
        //                case 4:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 10, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR244.Value, NoRMG244.Value, NoRMB244.Value, 252, 248, 244, NoRMR252.Value, NoRMG252.Value, NoRMB252.Value, NoRMR248.Value, NoRMG248.Value, NoRMB248.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR244.Value = r;
        //                        NoRMG244.Value = g;
        //                        NoRMB244.Value = b;
        //                        auto_gamma_write(gamma_r[26], Convert.ToUInt16(NoRMR244.Value), gamma_g[26], Convert.ToUInt16(NoRMG244.Value), gamma_b[26], Convert.ToUInt16(NoRMB244.Value));

        //                    }
        //                    break;
        //                case 5:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR236.Value, NoRMG236.Value, NoRMB236.Value, 248, 244, 236, NoRMR248.Value, NoRMG248.Value, NoRMB248.Value, NoRMR244.Value, NoRMG244.Value, NoRMB244.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR236.Value = r;
        //                        NoRMG236.Value = g;
        //                        NoRMB236.Value = b;
        //                        auto_gamma_write(gamma_r[25], Convert.ToUInt16(NoRMR236.Value), gamma_g[25], Convert.ToUInt16(NoRMG236.Value), gamma_b[25], Convert.ToUInt16(NoRMB236.Value));


        //                    }
        //                    break;
        //                case 6:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR228.Value, NoRMG228.Value, NoRMB228.Value, 244, 236, 228, NoRMR244.Value, NoRMG244.Value, NoRMB244.Value, NoRMR236.Value, NoRMG236.Value, NoRMB236.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR228.Value = r;
        //                        NoRMG228.Value = g;
        //                        NoRMB228.Value = b;
        //                        auto_gamma_write(gamma_r[24], Convert.ToUInt16(NoRMR228.Value), gamma_g[24], Convert.ToUInt16(NoRMG228.Value), gamma_b[24], Convert.ToUInt16(NoRMB228.Value));

        //                    }
        //                    break;
        //                case 7:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR220.Value, NoRMG220.Value, NoRMB220.Value, 236, 228, 220, NoRMR236.Value, NoRMG236.Value, NoRMB236.Value, NoRMR228.Value, NoRMG228.Value, NoRMB228.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR220.Value = r;
        //                        NoRMG220.Value = g;
        //                        NoRMB220.Value = b;
        //                        auto_gamma_write(gamma_r[23], Convert.ToUInt16(NoRMR220.Value), gamma_g[23], Convert.ToUInt16(NoRMG220.Value), gamma_b[23], Convert.ToUInt16(NoRMB220.Value));

        //                    }
        //                    break;
        //                case 8:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR212.Value, NoRMG212.Value, NoRMB212.Value, 228, 220, 212, NoRMR228.Value, NoRMG228.Value, NoRMB228.Value, NoRMR220.Value, NoRMG220.Value, NoRMB220.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR212.Value = r;
        //                        NoRMG212.Value = g;
        //                        NoRMB212.Value = b;
        //                        auto_gamma_write(gamma_r[22], Convert.ToUInt16(NoRMR212.Value), gamma_g[22], Convert.ToUInt16(NoRMG212.Value), gamma_b[22], Convert.ToUInt16(NoRMB212.Value));

        //                    }
        //                    break;
        //                case 9:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR204.Value, NoRMG204.Value, NoRMB204.Value, 220, 212, 204, NoRMR220.Value, NoRMG220.Value, NoRMB220.Value, NoRMR212.Value, NoRMG212.Value, NoRMB212.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR204.Value = r;
        //                        NoRMG204.Value = g;
        //                        NoRMB204.Value = b;
        //                        auto_gamma_write(gamma_r[21], Convert.ToUInt16(NoRMR204.Value), gamma_g[21], Convert.ToUInt16(NoRMG204.Value), gamma_b[21], Convert.ToUInt16(NoRMB204.Value));

        //                    }
        //                    break;
        //                case 10:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR196.Value, NoRMG196.Value, NoRMB196.Value, 212, 204, 196, NoRMR212.Value, NoRMG212.Value, NoRMB212.Value, NoRMR204.Value, NoRMG204.Value, NoRMB204.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR196.Value = r;
        //                        NoRMG196.Value = g;
        //                        NoRMB196.Value = b;
        //                        auto_gamma_write(gamma_r[20], Convert.ToUInt16(NoRMR196.Value), gamma_g[20], Convert.ToUInt16(NoRMG196.Value), gamma_b[20], Convert.ToUInt16(NoRMB196.Value));

        //                    }
        //                    break;
        //                case 11:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR188.Value, NoRMG188.Value, NoRMB188.Value, 204, 196, 188, NoRMR204.Value, NoRMG204.Value, NoRMB204.Value, NoRMR196.Value, NoRMG196.Value, NoRMB196.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR188.Value = r;
        //                        NoRMG188.Value = g;
        //                        NoRMB188.Value = b;
        //                        auto_gamma_write(gamma_r[19], Convert.ToUInt16(NoRMR188.Value), gamma_g[19], Convert.ToUInt16(NoRMG188.Value), gamma_b[19], Convert.ToUInt16(NoRMB188.Value));

        //                    }
        //                    break;
        //                case 12:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR180.Value, NoRMG180.Value, NoRMB180.Value, 196, 188, 180, NoRMR196.Value, NoRMG196.Value, NoRMB196.Value, NoRMR188.Value, NoRMG188.Value, NoRMB188.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR180.Value = r;
        //                        NoRMG180.Value = g;
        //                        NoRMB180.Value = b;
        //                        auto_gamma_write(gamma_r[18], Convert.ToUInt16(NoRMR180.Value), gamma_g[18], Convert.ToUInt16(NoRMG180.Value), gamma_b[18], Convert.ToUInt16(NoRMB180.Value));

        //                    }
        //                    break;
        //                case 13:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR164.Value, NoRMG164.Value, NoRMB164.Value, 188, 180, 164, NoRMR188.Value, NoRMG188.Value, NoRMB188.Value, NoRMR180.Value, NoRMG180.Value, NoRMB180.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR164.Value = r;
        //                        NoRMG164.Value = g;
        //                        NoRMB164.Value = b;
        //                        auto_gamma_write(gamma_r[17], Convert.ToUInt16(NoRMR164.Value), gamma_g[17], Convert.ToUInt16(NoRMG164.Value), gamma_b[17], Convert.ToUInt16(NoRMB164.Value));

        //                    }
        //                    break;
        //                case 14:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR148.Value, NoRMG148.Value, NoRMB148.Value, 180, 164, 148, NoRMR180.Value, NoRMG180.Value, NoRMB180.Value, NoRMR164.Value, NoRMG164.Value, NoRMB164.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR148.Value = r;
        //                        NoRMG148.Value = g;
        //                        NoRMB148.Value = b;
        //                        auto_gamma_write(gamma_r[16], Convert.ToUInt16(NoRMR148.Value), gamma_g[16], Convert.ToUInt16(NoRMG148.Value), gamma_b[16], Convert.ToUInt16(NoRMB148.Value));

        //                    }
        //                    break;
        //                case 15:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR132.Value, NoRMG132.Value, NoRMB132.Value, 164, 148, 132, NoRMR164.Value, NoRMG164.Value, NoRMB164.Value, NoRMR148.Value, NoRMG148.Value, NoRMB148.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR132.Value = r;
        //                        NoRMG132.Value = g;
        //                        NoRMB132.Value = b;
        //                        auto_gamma_write(gamma_r[15], Convert.ToUInt16(NoRMR132.Value), gamma_g[15], Convert.ToUInt16(NoRMG132.Value), gamma_b[15], Convert.ToUInt16(NoRMB132.Value));

        //                    }
        //                    break;
        //                case 16:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR116.Value, NoRMG116.Value, NoRMB116.Value, 148, 132, 116, NoRMR148.Value, NoRMG148.Value, NoRMB148.Value, NoRMR132.Value, NoRMG132.Value, NoRMB132.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR116.Value = r;
        //                        NoRMG116.Value = g;
        //                        NoRMB116.Value = b;
        //                        auto_gamma_write(gamma_r[14], Convert.ToUInt16(NoRMR116.Value), gamma_g[14], Convert.ToUInt16(NoRMG116.Value), gamma_b[14], Convert.ToUInt16(NoRMB116.Value));

        //                    }
        //                    break;
        //                case 17:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR100.Value, NoRMG100.Value, NoRMB100.Value, 132, 116, 100, NoRMR132.Value, NoRMG132.Value, NoRMB132.Value, NoRMR116.Value, NoRMG116.Value, NoRMB116.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR100.Value = r;
        //                        NoRMG100.Value = g;
        //                        NoRMB100.Value = b;
        //                        auto_gamma_write(gamma_r[13], Convert.ToUInt16(NoRMR100.Value), gamma_g[13], Convert.ToUInt16(NoRMG100.Value), gamma_b[13], Convert.ToUInt16(NoRMB100.Value));

        //                    }
        //                    break;
        //                case 18:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR84.Value, NoRMG84.Value, NoRMB84.Value, 116, 100, 84, NoRMR116.Value, NoRMG116.Value, NoRMB116.Value, NoRMR100.Value, NoRMG100.Value, NoRMB100.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR84.Value = r;
        //                        NoRMG84.Value = g;
        //                        NoRMB84.Value = b;
        //                        auto_gamma_write(gamma_r[12], Convert.ToUInt16(NoRMR84.Value), gamma_g[12], Convert.ToUInt16(NoRMG84.Value), gamma_b[12], Convert.ToUInt16(NoRMB84.Value));

        //                    }
        //                    break;
        //                case 19:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 5, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR68.Value, NoRMG68.Value, NoRMB68.Value, 100, 84, 68, NoRMR100.Value, NoRMG100.Value, NoRMB100.Value, NoRMR84.Value, NoRMG84.Value, NoRMB84.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR68.Value = r;
        //                        NoRMG68.Value = g;
        //                        NoRMB68.Value = b;
        //                        auto_gamma_write(gamma_r[11], Convert.ToUInt16(NoRMR68.Value), gamma_g[11], Convert.ToUInt16(NoRMG68.Value), gamma_b[11], Convert.ToUInt16(NoRMB68.Value));

        //                    }
        //                    break;
        //                case 20:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR52.Value, NoRMG52.Value, NoRMB52.Value, 84, 68, 52, NoRMR84.Value, NoRMG84.Value, NoRMB84.Value, NoRMR68.Value, NoRMG68.Value, NoRMB68.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR52.Value = r;
        //                        NoRMG52.Value = g;
        //                        NoRMB52.Value = b;
        //                        auto_gamma_write(gamma_r[10], Convert.ToUInt16(NoRMR52.Value), gamma_g[10], Convert.ToUInt16(NoRMG52.Value), gamma_b[10], Convert.ToUInt16(NoRMB52.Value));

        //                    }
        //                    break;
        //                case 21:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 40, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR44.Value, NoRMG44.Value, NoRMB44.Value, 68, 52, 44, NoRMR68.Value, NoRMG68.Value, NoRMB68.Value, NoRMR52.Value, NoRMG52.Value, NoRMB52.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR44.Value = r;
        //                        NoRMG44.Value = g;
        //                        NoRMB44.Value = b;
        //                        auto_gamma_write(gamma_r[9], Convert.ToUInt16(NoRMR44.Value), gamma_g[9], Convert.ToUInt16(NoRMG44.Value), gamma_b[9], Convert.ToUInt16(NoRMB44.Value));

        //                    }
        //                    break;
        //                case 22:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR36.Value, NoRMG36.Value, NoRMB36.Value, 52, 44, 36, NoRMR52.Value, NoRMG52.Value, NoRMB52.Value, NoRMR44.Value, NoRMG44.Value, NoRMB44.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR36.Value = r;
        //                        NoRMG36.Value = g;
        //                        NoRMB36.Value = b;
        //                        auto_gamma_write(gamma_r[8], Convert.ToUInt16(NoRMR36.Value), gamma_g[8], Convert.ToUInt16(NoRMG36.Value), gamma_b[8], Convert.ToUInt16(NoRMB36.Value));

        //                    }
        //                    break;
        //                case 23:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR28.Value, NoRMG28.Value, NoRMB28.Value, 44, 36, 28, NoRMR44.Value, NoRMG44.Value, NoRMB44.Value, NoRMR36.Value, NoRMG36.Value, NoRMB36.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR28.Value = r;
        //                        NoRMG28.Value = g;
        //                        NoRMB28.Value = b;
        //                        auto_gamma_write(gamma_r[7], Convert.ToUInt16(NoRMR28.Value), gamma_g[7], Convert.ToUInt16(NoRMG28.Value), gamma_b[7], Convert.ToUInt16(NoRMB28.Value));

        //                    }
        //                    break;
        //                case 24://20阶以下
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 2, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR20.Value, NoRMG20.Value, NoRMB20.Value, 36, 28, 20, NoRMR36.Value, NoRMG36.Value, NoRMB36.Value, NoRMR28.Value, NoRMG28.Value, NoRMB28.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR20.Value = r;
        //                        NoRMG20.Value = g;
        //                        NoRMB20.Value = b;
        //                        auto_gamma_write(gamma_r[6], Convert.ToUInt16(NoRMR20.Value), gamma_g[6], Convert.ToUInt16(NoRMG20.Value), gamma_b[6], Convert.ToUInt16(NoRMB20.Value));

        //                    }
        //                    break;
        //                case 25:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 1, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR16.Value, NoRMG16.Value, NoRMB16.Value, 28, 20, 16, NoRMR28.Value, NoRMG28.Value, NoRMB28.Value, NoRMR20.Value, NoRMG20.Value, NoRMB20.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR16.Value = r;
        //                        NoRMG16.Value = g;
        //                        NoRMB16.Value = b;
        //                        auto_gamma_write(gamma_r[5], Convert.ToUInt16(NoRMR16.Value), gamma_g[5], Convert.ToUInt16(NoRMG16.Value), gamma_b[5], Convert.ToUInt16(NoRMB16.Value));

        //                    }
        //                    break;
        //                case 26:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 30, 1, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR12.Value, NoRMG12.Value, NoRMB12.Value, 20, 16, 12, NoRMR20.Value, NoRMG20.Value, NoRMB20.Value, NoRMR16.Value, NoRMG16.Value, NoRMB16.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR12.Value = r;
        //                        NoRMG12.Value = g;
        //                        NoRMB12.Value = b;
        //                        auto_gamma_write(gamma_r[4], Convert.ToUInt16(NoRMR12.Value), gamma_g[4], Convert.ToUInt16(NoRMG12.Value), gamma_b[4], Convert.ToUInt16(NoRMB12.Value));

        //                    }
        //                    break;
        //                case 27:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 20, 1, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR8.Value, NoRMG8.Value, NoRMB8.Value, 16, 12, 8, NoRMR16.Value, NoRMG16.Value, NoRMB16.Value, NoRMR12.Value, NoRMG12.Value, NoRMB12.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR8.Value = r;
        //                        NoRMG8.Value = g;
        //                        NoRMB8.Value = b;
        //                        auto_gamma_write(gamma_r[3], Convert.ToUInt16(NoRMR8.Value), gamma_g[3], Convert.ToUInt16(NoRMG8.Value), gamma_b[3], Convert.ToUInt16(NoRMB8.Value));

        //                    }
        //                    break;
        //                case 28:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 1, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], flexible_gamma_count, NoRMR4.Value, NoRMG4.Value, NoRMB4.Value, 12, 8, 4, NoRMR12.Value, NoRMG12.Value, NoRMB12.Value, NoRMR8.Value, NoRMG8.Value, NoRMB8.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR4.Value = r;
        //                        NoRMG4.Value = g;
        //                        NoRMB4.Value = b;
        //                        auto_gamma_write(gamma_r[2], Convert.ToUInt16(NoRMR4.Value), gamma_g[2], Convert.ToUInt16(NoRMG4.Value), gamma_b[2], Convert.ToUInt16(NoRMB4.Value));

        //                    }
        //                    break;
        //                case 29:
        //                    {
        //                        R6645x_Gamma_auto_Adujst(flag1, flag3, flag2, 10, 1, 1, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], 2, NoRMR3.Value, NoRMG3.Value, NoRMB3.Value, 8, 4, 3, NoRMR8.Value, NoRMG8.Value, NoRMB8.Value, NoRMR4.Value, NoRMG4.Value, NoRMB4.Value, out r, out g, out b, out Gamma_adjust_timeout_flag);
        //                        if (Gamma_adjust_timeout_flag == true)
        //                        {
        //                            flag1 = 0; flag3 = 0; flag2 = 0;
        //                        }
        //                        NoRMR3.Value = r;
        //                        NoRMG3.Value = g;
        //                        NoRMB3.Value = b;
        //                        auto_gamma_write(gamma_r[1], Convert.ToUInt16(NoRMR3.Value), gamma_g[1], Convert.ToUInt16(NoRMG3.Value), GAMMAs.RM195_Gamma_b[1], Convert.ToUInt16(NoRMB3.Value));

        //                    }
        //                    break;
        //            }
        //        }
        //        catch
        //        {
        //            T5.Enabled = false;
        //            MessageBox.Show("数值超限！");
        //        }
        //    }
        //}
        private void btnCalculate350_Click(object sender, EventArgs e)
        {
            txt_350_255.Text = (Math.Pow((Parttern_RM69310[0] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_239.Text = (Math.Pow((Parttern_RM69310[1] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_223.Text = (Math.Pow((Parttern_RM69310[2] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_207.Text = (Math.Pow((Parttern_RM69310[3] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_191.Text = (Math.Pow((Parttern_RM69310[4] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_175.Text = (Math.Pow((Parttern_RM69310[5] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_159.Text = (Math.Pow((Parttern_RM69310[6] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_143.Text = (Math.Pow((Parttern_RM69310[7] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_127.Text = (Math.Pow((Parttern_RM69310[8] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_111.Text = (Math.Pow((Parttern_RM69310[9] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_95.Text = (Math.Pow((Parttern_RM69310[10] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_79.Text = (Math.Pow((Parttern_RM69310[11] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_63.Text = (Math.Pow((Parttern_RM69310[12] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_55.Text = (Math.Pow((Parttern_RM69310[13] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_47.Text = (Math.Pow((Parttern_RM69310[14] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_39.Text = (Math.Pow((Parttern_RM69310[15] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_35.Text = (Math.Pow((Parttern_RM69310[16] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_31.Text = (Math.Pow((Parttern_RM69310[17] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_27.Text = (Math.Pow((Parttern_RM69310[18] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_23.Text = (Math.Pow((Parttern_RM69310[19] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_19.Text = (Math.Pow((Parttern_RM69310[20] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_15.Text = (Math.Pow((Parttern_RM69310[21] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_11.Text = (Math.Pow((Parttern_RM69310[22] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_7.Text = (Math.Pow((Parttern_RM69310[23] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            //txt_350_1.Text = (Math.Pow((Parttern_RM69310[24] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            ////0灰阶更换为对比度典型值计算。
            txt_350_1.Text = (Convert.ToDouble(txt_350_l.Text) / Convert.ToDouble(textBox_CR.Text)).ToString("F4");
        }




    }
}
